Add rounding compensation while rendering track

This commit is contained in:
Loch Christian (uib05376) 2020-11-12 15:12:10 +01:00
parent 9cad2f20f1
commit 457c9662c3

View File

@ -5,7 +5,6 @@ import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import java.util.ConcurrentModificationException; import java.util.ConcurrentModificationException;
import java.util.List; import java.util.List;
@ -14,6 +13,7 @@ public class TimeRecordView extends View {
protected Paint paint; protected Paint paint;
protected Track track; protected Track track;
protected int pixelPerVehicle; protected int pixelPerVehicle;
protected float tooShortPerTrackLength;
public TimeRecordView(Context context, Track track) { public TimeRecordView(Context context, Track track) {
super(context); super(context);
@ -23,6 +23,7 @@ public class TimeRecordView extends View {
paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setStyle(Paint.Style.FILL_AND_STROKE);
this.setBackgroundColor(Color.BLACK); this.setBackgroundColor(Color.BLACK);
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength()); this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
this.tooShortPerTrackLength = (this.getWidth() - this.pixelPerVehicle*this.track.getTrackLength())/this.track.getTrackLength();
} }
protected int getColor(float curVelocity, float maxVelocity) { protected int getColor(float curVelocity, float maxVelocity) {
@ -52,6 +53,7 @@ public class TimeRecordView extends View {
protected void onSizeChanged(int w, int h, int oldw, int oldh) { protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh); super.onSizeChanged(w, h, oldw, oldh);
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength()); this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
this.tooShortPerTrackLength = (this.getWidth() - this.pixelPerVehicle*this.track.getTrackLength())/this.track.getTrackLength();
} }
@Override @Override
@ -64,8 +66,9 @@ public class TimeRecordView extends View {
int i = 0; int i = 0;
for (VehicleTimeRecord r : step) { for (VehicleTimeRecord r : step) {
int left = (int) (this.pixelPerVehicle * r.getPosition()); int left = (int) (this.pixelPerVehicle * r.getPosition());
int compensate = Math.round(r.getPosition()*this.tooShortPerTrackLength);
this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity())); this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
canvas.drawRect(left, y, left + this.pixelPerVehicle - 1, canvas.drawRect(left+compensate, y, left + this.pixelPerVehicle - 1+compensate,
y + 10, this.paint); y + 10, this.paint);
i++; i++;
} }