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.

74 lines
2.5KB

  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 Track track;
  12. protected int pixelPerVehicle;
  13. public TimeRecordView(Context context, Track track) {
  14. super(context);
  15. this.track = track;
  16. this.paint = new Paint();
  17. paint.setColor(Color.BLACK);
  18. paint.setStyle(Paint.Style.FILL_AND_STROKE);
  19. this.setBackgroundColor(Color.BLACK);
  20. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  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, 10*50);
  40. setMeasuredDimension(widthMeasureSpec, 10*50);
  41. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  42. }
  43. @Override
  44. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  45. super.onSizeChanged(w, h, oldw, oldh);
  46. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  47. }
  48. @Override
  49. protected void onDraw(Canvas canvas) {
  50. int y = 0;
  51. List<List<VehicleTimeRecord>> stepList = this.track.getVtrList();
  52. for (int curStepIdx = stepList.size()-1; curStepIdx >= stepList.size()-1-50 && curStepIdx >= 0; curStepIdx--) {
  53. List<VehicleTimeRecord> step = stepList.get(curStepIdx);
  54. int i = 0;
  55. for (VehicleTimeRecord r : step) {
  56. int left = (int) (this.pixelPerVehicle * r.getPosition());
  57. this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
  58. canvas.drawRect(left, y, left + this.pixelPerVehicle - 1,
  59. y+10, this.paint);
  60. i++;
  61. }
  62. y += 10;
  63. }
  64. }
  65. }