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.

84 lines
3.2KB

  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 java.util.ConcurrentModificationException;
  8. import java.util.List;
  9. public class TimeRecordView extends View {
  10. protected Paint paint;
  11. protected Track track;
  12. protected int pixelPerVehicle;
  13. protected float tooShortPerTrackLength;
  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. this.tooShortPerTrackLength = (this.getWidth() - this.pixelPerVehicle*this.track.getTrackLength())/this.track.getTrackLength();
  23. }
  24. protected int getColor(float curVelocity, float maxVelocity) {
  25. float perc = curVelocity / maxVelocity;
  26. perc = 1 - perc;
  27. if (perc <= 0.5) {
  28. int red = ((int) (2*perc*0xFF)) << 16;
  29. int green = ((int)0xFF) << 8;
  30. int blue = 0;
  31. return 0xff000000 | red | green | blue;
  32. } else {
  33. int red = ((int)(0xFF)) << 16;
  34. int green = ((int)(0xFF-0xFF*(perc-0.5)*2)) << 8;
  35. int blue = 0;
  36. return 0xff000000 | red | green | blue;
  37. }
  38. }
  39. @Override
  40. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  41. super.onMeasure(widthMeasureSpec, 10*50);
  42. setMeasuredDimension(widthMeasureSpec, 10*50);
  43. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  44. }
  45. @Override
  46. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  47. super.onSizeChanged(w, h, oldw, oldh);
  48. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  49. this.tooShortPerTrackLength = (this.getWidth() - this.pixelPerVehicle*this.track.getTrackLength())/this.track.getTrackLength();
  50. }
  51. @Override
  52. protected void onDraw(Canvas canvas) {
  53. int y = 0;
  54. List<List<VehicleTimeRecord>> stepList = this.track.getVtrList();
  55. for (int curStepIdx = stepList.size()-1; curStepIdx >= stepList.size()-1-50 && curStepIdx >= 0; curStepIdx--) {
  56. try {
  57. List<VehicleTimeRecord> step = stepList.get(curStepIdx);
  58. int i = 0;
  59. for (VehicleTimeRecord r : step) {
  60. int left = (int) (this.pixelPerVehicle * r.getPosition());
  61. int compensate = Math.round(r.getPosition()*this.tooShortPerTrackLength);
  62. this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
  63. canvas.drawRect(left+compensate, y, left + this.pixelPerVehicle - 1+compensate,
  64. y + 10, this.paint);
  65. i++;
  66. }
  67. y += 10;
  68. } catch (ConcurrentModificationException ex) {
  69. System.out.println("Concurrent Exception occured, skipping record");
  70. y += 10;
  71. continue;
  72. }
  73. }
  74. }
  75. }