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.

97 lines
3.6KB

  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. protected int historyLength;
  15. public TimeRecordView(Context context, Track track, int historyLength) {
  16. super(context);
  17. this.track = track;
  18. this.paint = new Paint();
  19. this.historyLength = historyLength;
  20. paint.setColor(Color.BLACK);
  21. paint.setStyle(Paint.Style.FILL_AND_STROKE);
  22. this.setBackgroundColor(Color.BLACK);
  23. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  24. this.tooShortPerTrackLength = (this.getWidth() - this.pixelPerVehicle*this.track.getTrackLength())/this.track.getTrackLength();
  25. }
  26. public void setTrack(Track track) {
  27. this.track = track;
  28. }
  29. protected int getColor(float curVelocity, float maxVelocity) {
  30. float perc = curVelocity / maxVelocity;
  31. perc = 1 - perc;
  32. if (perc <= 0.5) {
  33. int red = ((int) (2*perc*0xFF)) << 16;
  34. int green = ((int)0xFF) << 8;
  35. int blue = 0;
  36. return 0xff000000 | red | green | blue;
  37. } else {
  38. int red = ((int)(0xFF)) << 16;
  39. int green = ((int)(0xFF-0xFF*(perc-0.5)*2)) << 8;
  40. int blue = 0;
  41. return 0xff000000 | red | green | blue;
  42. }
  43. }
  44. @Override
  45. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  46. super.onMeasure(widthMeasureSpec, 10*50);
  47. setMeasuredDimension(widthMeasureSpec, 10*50);
  48. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  49. }
  50. @Override
  51. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  52. super.onSizeChanged(w, h, oldw, oldh);
  53. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  54. this.tooShortPerTrackLength = (this.getWidth() - this.pixelPerVehicle*this.track.getTrackLength())/this.track.getTrackLength();
  55. }
  56. @Override
  57. protected void onDraw(Canvas canvas) {
  58. int y = 0;
  59. List<List<VehicleTimeRecord>> stepList = this.track.getVtrList();
  60. for (int curStepIdx = this.historyLength-1;
  61. curStepIdx >= 0 && stepList.size() >= curStepIdx;
  62. curStepIdx--) {
  63. try {
  64. try {
  65. track.getListSemaphore().acquire();
  66. } catch (InterruptedException ex) { return; }
  67. List<VehicleTimeRecord> step = stepList.get(curStepIdx);
  68. int i = 0;
  69. for (VehicleTimeRecord r : step) {
  70. int left = (int) (this.pixelPerVehicle * r.getPosition());
  71. int compensate = Math.round(r.getPosition()*this.tooShortPerTrackLength);
  72. this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
  73. canvas.drawRect(left+compensate, y,
  74. left + this.pixelPerVehicle - 1+compensate,
  75. y + 10, this.paint);
  76. i++;
  77. }
  78. track.getListSemaphore().release();
  79. y += 10;
  80. } catch (ConcurrentModificationException ex) {
  81. System.out.println("Concurrent Exception occured, skipping record");
  82. y += 10;
  83. continue;
  84. }
  85. }
  86. }
  87. }