|
- 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.List;
-
- public class TimeRecordView extends View {
- protected Paint paint;
- protected List<VehicleTimeRecord> records;
- protected int pixelPerVehicle;
- protected float trackLength;
-
- public TimeRecordView(Context context, List<VehicleTimeRecord> records, float trackLength) {
- super(context);
- this.records = records;
- this.trackLength = trackLength;
- this.paint = new Paint();
- paint.setColor(Color.BLACK);
- paint.setStyle(Paint.Style.FILL_AND_STROKE);
- }
-
- 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, 20);
- setMeasuredDimension(widthMeasureSpec, 20);
- }
-
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- this.pixelPerVehicle = (int) (this.getWidth() / this.trackLength);
- int i = 0;
- for (VehicleTimeRecord r : this.records) {
- int left = (int) (this.pixelPerVehicle * r.getPosition());
- this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
- canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1,
- 20, this.paint);
- i++;
- }
- }
- }
|