DrawView.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package de.tu_darmstadt.informatik.tk.olir;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.os.Handler;
  7. import android.util.AttributeSet;
  8. import android.util.TypedValue;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import java.util.ArrayList;
  12. /**
  13. * Area where the object's shape gets drawn
  14. * Created by Martin Herbers on 30.03.2017.
  15. */
  16. public class DrawView extends View {
  17. /**
  18. * List of Strings where every String describes a basic shape, e.g. rectangle: [R;0;0;20;20]
  19. */
  20. ArrayList<String> shapes;
  21. /**
  22. * millimeter * scale = pixel
  23. */
  24. private float scale;
  25. private float electrodeX;
  26. private float electrodeY;
  27. private float electrodeWidth;
  28. private float electrodeHeight;
  29. private boolean statusDetected = false;
  30. private Handler handler;
  31. public DrawView(Context context) {
  32. super(context);
  33. clearShapes();
  34. }
  35. public DrawView(Context context, AttributeSet attribs) {
  36. super(context, attribs);
  37. clearShapes();
  38. }
  39. public DrawView(Context context, AttributeSet attribs, int defStyle) {
  40. super(context, attribs, defStyle);
  41. clearShapes();
  42. }
  43. @Override
  44. public boolean onTouchEvent(MotionEvent event) {
  45. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  46. float touchX = event.getX();
  47. float touchY = event.getY();
  48. float baseX = getWidth()/2.0f;
  49. float baseY = getHeight()/2.0f;
  50. //Looking for width and height instead of width/2 nad height/2 in both direction is intended.
  51. //This functions as a larger area to catch the input if the user doesn't place the object perfectly
  52. if (touchX > baseX + (electrodeX - electrodeWidth) * scale &&
  53. touchX < baseX + (electrodeX + electrodeWidth) * scale &&
  54. touchY > baseY + (electrodeY - electrodeHeight) * scale &&
  55. touchY < baseY + (electrodeY + electrodeHeight) * scale) {
  56. statusDetected = true;
  57. }
  58. }
  59. if (handler == null) {
  60. handler = new Handler();
  61. handler.postDelayed(new Runnable() {
  62. @Override
  63. public void run() {
  64. checkStatus();
  65. }
  66. }, 300);
  67. }
  68. return super.onTouchEvent(event);
  69. }
  70. private void checkStatus() {
  71. ReadingActivity activity = (ReadingActivity) getContext();
  72. if (statusDetected)
  73. activity.onDetected(null);
  74. else
  75. activity.onNotDetected(null);
  76. }
  77. @Override
  78. public void onDraw(Canvas canvas) {
  79. scale = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 1,
  80. getResources().getDisplayMetrics());
  81. //Middle point of the draw area
  82. float baseX = canvas.getWidth()/2.0f;
  83. float baseY = canvas.getHeight()/2.0f;
  84. Paint paint = new Paint();
  85. paint.setStyle(Paint.Style.FILL);
  86. paint.setColor(Color.LTGRAY);
  87. for (String s: shapes) {
  88. //Circle
  89. if (s.charAt(1) == 'C') {
  90. //X offset from middle point
  91. s = s.substring(3);
  92. int index = s.indexOf(';');
  93. float x = Float.parseFloat(s.substring(0, index));
  94. //Y offset from middle point
  95. s = s.substring(index + 1);
  96. index = s.indexOf(';');
  97. float y = Float.parseFloat(s.substring(0, index));
  98. //the circle's radius
  99. s = s.substring(index + 1);
  100. index = s.length()-1;
  101. float radius = Float.parseFloat(s.substring(0, index)) / 2;
  102. //Draw the circle shape
  103. canvas.drawCircle(baseX + x * scale,
  104. baseY + y * scale,
  105. radius * scale,
  106. paint);
  107. //Rectangle
  108. } else if (s.charAt(1) == 'R') {
  109. //X offset from middle point
  110. s = s.substring(3);
  111. int index = s.indexOf(';');
  112. float x = Float.parseFloat(s.substring(0, index));
  113. //Y offset from middle point
  114. s = s.substring(index + 1);
  115. index = s.indexOf(';');
  116. float y = Float.parseFloat(s.substring(0, index));
  117. //width
  118. s = s.substring(index + 1);
  119. index = s.indexOf(';');
  120. float width = Float.parseFloat(s.substring(0, index));
  121. //height
  122. s = s.substring(index + 1);
  123. index = s.length()-1;
  124. float height = Float.parseFloat(s.substring(0, index));
  125. //Draw the rectangle
  126. canvas.drawRect(baseX + x * scale - width / 2 * scale,
  127. baseY + y * scale - height / 2 * scale,
  128. baseX + x * scale + width / 2 * scale,
  129. baseY + y * scale + height / 2 * scale,
  130. paint);
  131. }
  132. }
  133. }
  134. /**
  135. * Clear the shapes list, use it every time a new object is loaded
  136. */
  137. public void clearShapes() {
  138. shapes = new ArrayList<>();
  139. }
  140. /**
  141. * Add a shape to the list
  142. * @param s the string describing a basic shape
  143. */
  144. public void addShape(String s) {
  145. shapes.add(s);
  146. }
  147. public void setPosition(float x, float y, float width, float height) {
  148. electrodeX = x;
  149. electrodeY = y;
  150. electrodeWidth = width;
  151. electrodeHeight = height;
  152. }
  153. }