DrawView.java 5.7 KB

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