You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1KB

  1. package de.hems.trafficsim;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import java.util.List;
  9. public class TimeRecordView extends View {
  10. protected Paint paint;
  11. protected List<VehicleTimeRecord> records;
  12. protected int pixelPerVehicle;
  13. protected float trackLength;
  14. public TimeRecordView(Context context, List<VehicleTimeRecord> records, float trackLength) {
  15. super(context);
  16. this.records = records;
  17. this.trackLength = trackLength;
  18. this.paint = new Paint();
  19. paint.setColor(Color.BLACK);
  20. paint.setStyle(Paint.Style.FILL_AND_STROKE);
  21. }
  22. protected int getColor(float curVelocity, float maxVelocity) {
  23. float perc = curVelocity / maxVelocity;
  24. perc = 1 - perc;
  25. if (perc <= 0.5) {
  26. int red = ((int) (2*perc*0xFF)) << 16;
  27. int green = ((int)0xFF) << 8;
  28. int blue = 0;
  29. return 0xff000000 | red | green | blue;
  30. } else {
  31. int red = ((int)(0xFF)) << 16;
  32. int green = ((int)(0xFF-0xFF*(perc-0.5)*2)) << 8;
  33. int blue = 0;
  34. return 0xff000000 | red | green | blue;
  35. }
  36. }
  37. @Override
  38. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  39. super.onMeasure(widthMeasureSpec, 20);
  40. setMeasuredDimension(widthMeasureSpec, 20);
  41. }
  42. @Override
  43. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  44. super.onSizeChanged(w, h, oldw, oldh);
  45. }
  46. @Override
  47. protected void onDraw(Canvas canvas) {
  48. this.pixelPerVehicle = (int) (this.getWidth() / this.trackLength);
  49. int i = 0;
  50. for (VehicleTimeRecord r : this.records) {
  51. int left = (int) (this.pixelPerVehicle * r.getPosition());
  52. this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
  53. canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1,
  54. 20, this.paint);
  55. i++;
  56. }
  57. }
  58. }