Advertisement
Guest User

Untitled

a guest
May 9th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 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. break;
  83. }
  84. case MotionEvent.ACTION_POINTER_DOWN:
  85. {
  86. final float x = ev.getX();
  87. final float y = ev.getY();
  88.  
  89. touch_start(x, y);
  90.  
  91. mActivePointerId = ev.getPointerId(0);
  92. break;
  93. }
  94. case MotionEvent.ACTION_MOVE:
  95. {
  96. // Find the index of the active pointer and fetch its position
  97. final int pointerIndex = ev.findPointerIndex(mActivePointerId);
  98. final float x = ev.getX(pointerIndex);
  99. final float y = ev.getY(pointerIndex);
  100.  
  101. touch_move(x, y);
  102. break;
  103. }
  104. case MotionEvent.ACTION_UP:
  105. {
  106. mActivePointerId = INVALID_POINTER_ID;
  107. break;
  108. }
  109. case MotionEvent.ACTION_CANCEL: {
  110. mActivePointerId = INVALID_POINTER_ID;
  111. break;
  112. }
  113. case MotionEvent.ACTION_POINTER_UP:
  114. {
  115. // Extract the index of the pointer that left the touch sensor
  116. final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK)
  117. >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  118. final int pointerId = ev.getPointerId(pointerIndex);
  119. if (pointerId == mActivePointerId)
  120. {
  121. // This was our active pointer going up. Choose a new
  122. // active pointer and adjust accordingly.
  123. final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
  124. mX = ev.getX(newPointerIndex);
  125. mY = ev.getY(newPointerIndex);
  126. mActivePointerId = ev.getPointerId(newPointerIndex);
  127. }
  128. touch_up();
  129. }
  130. break;
  131. case MotionEvent.ACTION_OUTSIDE:
  132. break;
  133. }
  134.  
  135. invalidate();
  136. return true;
  137. }
  138.  
  139. private void touch_start(float x, float y)
  140. {
  141. undonePaths.clear();
  142. m_Path.reset();
  143. m_Path.moveTo(x, y);
  144. mX = x;
  145. mY = y;
  146. }
  147.  
  148. private void touch_move(float x, float y)
  149. {
  150. float dx = Math.abs(x - mX);
  151. float dy = Math.abs(y - mY);
  152. if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
  153. {
  154. m_Path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
  155. mX = x;
  156. mY = y;
  157. }
  158. }
  159. private void touch_up()
  160. {
  161. m_Path.lineTo(mX, mY);
  162.  
  163. // commit the path to our offscreen
  164. m_Canvas.drawPath(m_Path, m_Paint);
  165.  
  166. // kill this so we don't double draw
  167. Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
  168. arrayListPaths.add(new Pair<Path, Paint>(m_Path, newPaint));
  169. m_Path = new Path();
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement