Advertisement
Guest User

Untitled

a guest
May 9th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. public class DrawView extends View
  2. {
  3. private Canvas m_Canvas;
  4.  
  5. private Path m_Path;
  6.  
  7. private Paint m_Paint;
  8.  
  9. ArrayList<Pair<Path, Paint>> arrayListPaths = new ArrayList<Pair<Path, Paint>>();
  10.  
  11. ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>();
  12.  
  13. private float mX, mY;
  14.  
  15. private Bitmap bitmapToCanvas;
  16.  
  17. private static final float TOUCH_TOLERANCE = 4;
  18.  
  19. public DrawView(Context context)
  20. {
  21. super(context);
  22. setFocusable(true);
  23. setFocusableInTouchMode(true);
  24.  
  25. onCanvasInitialization();
  26. }
  27.  
  28. public void onCanvasInitialization()
  29. {
  30. m_Paint = new Paint();
  31. m_Paint.setAntiAlias(true);
  32. m_Paint.setDither(true);
  33. m_Paint.setColor(Color.parseColor("#37A1D1"));
  34. m_Paint.setStyle(Paint.Style.STROKE);
  35. m_Paint.setStrokeJoin(Paint.Join.ROUND);
  36. m_Paint.setStrokeCap(Paint.Cap.ROUND);
  37. m_Paint.setStrokeWidth(2);
  38.  
  39. m_Path = new Path();
  40. }
  41.  
  42. @Override
  43. protected void onSizeChanged(int w, int h, int oldw, int oldh)
  44. {
  45. super.onSizeChanged(w, h, oldw, oldh);
  46.  
  47. bitmapToCanvas = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  48. m_Canvas = new Canvas(bitmapToCanvas);
  49. }
  50.  
  51. @Override
  52. protected void onDraw(Canvas canvas)
  53. {
  54. canvas.drawBitmap(bitmapToCanvas, 0f, 0f, null);
  55. canvas.drawPath(m_Path, m_Paint);
  56. }
  57.  
  58. public void onDrawCanvas()
  59. {
  60. for (Pair<Path, Paint> p : arrayListPaths)
  61. {
  62. m_Canvas.drawPath(p.first, p.second);
  63. }
  64. }
  65.  
  66. private static final int INVALID_POINTER_ID = -1;
  67.  
  68. // The ‘active pointer’ is the one currently moving our object.
  69. private int mActivePointerId = INVALID_POINTER_ID;
  70.  
  71. @Override
  72. public boolean onTouchEvent(MotionEvent ev)
  73. {
  74. super.onTouchEvent(ev);
  75.  
  76. final int action = ev.getAction();
  77.  
  78. switch (action & MotionEvent.ACTION_MASK)
  79. {
  80. case MotionEvent.ACTION_DOWN:
  81. {
  82. final float x = ev.getX();
  83. final float y = ev.getY();
  84.  
  85. touch_start(x, y);
  86.  
  87. mActivePointerId = ev.getPointerId(0);
  88.  
  89. break;
  90. }
  91. case MotionEvent.ACTION_POINTER_DOWN:
  92. {
  93. break;
  94. }
  95. case MotionEvent.ACTION_MOVE:
  96. {
  97. // Find the index of the active pointer and fetch its position
  98. final int pointerIndex = ev.findPointerIndex(mActivePointerId);
  99. final float x = ev.getX(pointerIndex);
  100. final float y = ev.getY(pointerIndex);
  101.  
  102. touch_move(x, y);
  103. break;
  104. }
  105. case MotionEvent.ACTION_UP:
  106. {
  107. mActivePointerId = INVALID_POINTER_ID;
  108. touch_up();
  109. break;
  110. }
  111. case MotionEvent.ACTION_CANCEL:
  112. {
  113. mActivePointerId = INVALID_POINTER_ID;
  114. break;
  115. }
  116. case MotionEvent.ACTION_POINTER_UP:
  117. {
  118. // Extract the index of the pointer that left the touch sensor
  119. final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK)
  120. >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  121. final int pointerId = ev.getPointerId(pointerIndex);
  122. if (pointerId == mActivePointerId)
  123. {
  124. // This was our active pointer going up. Choose a new
  125. // active pointer and adjust accordingly.
  126. final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
  127. mX = ev.getX(newPointerIndex);
  128. mY = ev.getY(newPointerIndex);
  129. mActivePointerId = ev.getPointerId(newPointerIndex);
  130. }
  131. touch_up();
  132. }
  133. break;
  134. case MotionEvent.ACTION_OUTSIDE:
  135. break;
  136. }
  137.  
  138. invalidate();
  139. return true;
  140. }
  141.  
  142. private void touch_start(float x, float y)
  143. {
  144. undonePaths.clear();
  145. m_Path.reset();
  146. m_Path.moveTo(x, y);
  147. mX = x;
  148. mY = y;
  149. }
  150.  
  151. private void touch_move(float x, float y)
  152. {
  153. float dx = Math.abs(x - mX);
  154. float dy = Math.abs(y - mY);
  155. if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
  156. {
  157. m_Path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
  158. mX = x;
  159. mY = y;
  160. }
  161. }
  162. private void touch_up()
  163. {
  164. m_Path.lineTo(mX, mY);
  165.  
  166. // commit the path to our offscreen
  167. m_Canvas.drawPath(m_Path, m_Paint);
  168.  
  169. // kill this so we don't double draw
  170. Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
  171. arrayListPaths.add(new Pair<Path, Paint>(m_Path, newPaint));
  172. m_Path = new Path();
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement