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.

68 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. this.setBackgroundColor(Color.BLACK);
  22. }
  23. protected int getColor(float curVelocity, float maxVelocity) {
  24. float perc = curVelocity / maxVelocity;
  25. perc = 1 - perc;
  26. if (perc <= 0.5) {
  27. int red = ((int) (2*perc*0xFF)) << 16;
  28. int green = ((int)0xFF) << 8;
  29. int blue = 0;
  30. return 0xff000000 | red | green | blue;
  31. } else {
  32. int red = ((int)(0xFF)) << 16;
  33. int green = ((int)(0xFF-0xFF*(perc-0.5)*2)) << 8;
  34. int blue = 0;
  35. return 0xff000000 | red | green | blue;
  36. }
  37. }
  38. @Override
  39. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  40. super.onMeasure(widthMeasureSpec, 10);
  41. setMeasuredDimension(widthMeasureSpec, 10);
  42. }
  43. @Override
  44. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  45. super.onSizeChanged(w, h, oldw, oldh);
  46. }
  47. @Override
  48. protected void onDraw(Canvas canvas) {
  49. this.pixelPerVehicle = (int) (this.getWidth() / this.trackLength);
  50. int i = 0;
  51. for (VehicleTimeRecord r : this.records) {
  52. int left = (int) (this.pixelPerVehicle * r.getPosition());
  53. this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
  54. canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1,
  55. 10, this.paint);
  56. i++;
  57. }
  58. }
  59. }