package de.hems.trafficsim; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; import android.view.ViewGroup; import java.util.ConcurrentModificationException; import java.util.List; public class TimeRecordView extends View { protected Paint paint; protected Track track; protected int pixelPerVehicle; public TimeRecordView(Context context, Track track) { super(context); this.track = track; this.paint = new Paint(); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.FILL_AND_STROKE); this.setBackgroundColor(Color.BLACK); this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength()); } protected int getColor(float curVelocity, float maxVelocity) { float perc = curVelocity / maxVelocity; perc = 1 - perc; if (perc <= 0.5) { int red = ((int) (2*perc*0xFF)) << 16; int green = ((int)0xFF) << 8; int blue = 0; return 0xff000000 | red | green | blue; } else { int red = ((int)(0xFF)) << 16; int green = ((int)(0xFF-0xFF*(perc-0.5)*2)) << 8; int blue = 0; return 0xff000000 | red | green | blue; } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, 10*50); setMeasuredDimension(widthMeasureSpec, 10*50); this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength()); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength()); } @Override protected void onDraw(Canvas canvas) { int y = 0; List> stepList = this.track.getVtrList(); for (int curStepIdx = stepList.size()-1; curStepIdx >= stepList.size()-1-50 && curStepIdx >= 0; curStepIdx--) { try { List step = stepList.get(curStepIdx); int i = 0; for (VehicleTimeRecord r : step) { int left = (int) (this.pixelPerVehicle * r.getPosition()); this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity())); canvas.drawRect(left, y, left + this.pixelPerVehicle - 1, y + 10, this.paint); i++; } y += 10; } catch (ConcurrentModificationException ex) { System.out.println("Concurrent Exception occured, skipping record"); continue; } } } }