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.

52 lines
1.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 android.view.ViewGroup;
  8. public class TrackView extends View {
  9. protected Paint paint;
  10. protected Track track;
  11. protected int pixelPerVehicle;
  12. public TrackView(Context context, Track track) {
  13. super(context);
  14. this.track = track;
  15. this.paint = new Paint();
  16. paint.setColor(Color.BLACK);
  17. paint.setStyle(Paint.Style.FILL_AND_STROKE);
  18. }
  19. protected int getColor(float curVelocity, float maxVelocity) {
  20. float perc = curVelocity / maxVelocity;
  21. if (perc <= 0.5) {
  22. int red = ((int) (2*perc*0xFF)) << 16;
  23. int green = ((int)0xFF) << 8;
  24. int blue = 0;
  25. return 0xff000000 | red | green | blue;
  26. } else {
  27. int red = ((int)(0xFF)) << 16;
  28. int green = ((int)(0xFF-0xFF*(perc-0.5)*2)) << 8;
  29. int blue = 0;
  30. return 0xff000000 | red | green | blue;
  31. }
  32. }
  33. @Override
  34. protected void onDraw(Canvas canvas) {
  35. this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
  36. //super.onDraw(canvas);
  37. int i = 0;
  38. for (Vehicle v : this.track.getVehicles()) {
  39. int left = (int) (this.pixelPerVehicle * v.getPosition());
  40. this.paint.setColor(getColor(v.getCurVelocity(), v.getMaxVelocity()));
  41. canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1,
  42. 20, this.paint);
  43. i++;
  44. }
  45. }
  46. }