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.

80 lines
2.8KB

  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.ConcurrentModificationException;
  9. import java.util.List;
  10. public class TimeRecordView extends View {
  11. protected Paint paint;
  12. protected Track track;
  13. protected int pixelPerVehicle;
  14. public TimeRecordView(Context context, Track track) {
  15. super(context);
  16. this.track = track;
  17. this.paint = new Paint();
  18. paint.setColor(Color.BLACK);
  19. paint.setStyle(Paint.Style.FILL_AND_STROKE);
  20. this.setBackgroundColor(Color.BLACK);
  21. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  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*50);
  41. setMeasuredDimension(widthMeasureSpec, 10*50);
  42. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  43. }
  44. @Override
  45. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  46. super.onSizeChanged(w, h, oldw, oldh);
  47. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  48. }
  49. @Override
  50. protected void onDraw(Canvas canvas) {
  51. int y = 0;
  52. List<List<VehicleTimeRecord>> stepList = this.track.getVtrList();
  53. for (int curStepIdx = stepList.size()-1; curStepIdx >= stepList.size()-1-50 && curStepIdx >= 0; curStepIdx--) {
  54. try {
  55. List<VehicleTimeRecord> step = stepList.get(curStepIdx);
  56. int i = 0;
  57. for (VehicleTimeRecord r : step) {
  58. int left = (int) (this.pixelPerVehicle * r.getPosition());
  59. this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
  60. canvas.drawRect(left, y, left + this.pixelPerVehicle - 1,
  61. y + 10, this.paint);
  62. i++;
  63. }
  64. y += 10;
  65. } catch (ConcurrentModificationException ex) {
  66. System.out.println("Concurrent Exception occured, skipping record");
  67. continue;
  68. }
  69. }
  70. }
  71. }