Advertisement
Guest User

Untitled

a guest
May 2nd, 2013
1,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. public class DrawView extends View implements OnTouchListener
  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. this.setOnTouchListener(this);
  25.  
  26. onCanvasInitialization();
  27. }
  28.  
  29. public void onCanvasInitialization()
  30. {
  31. m_Paint = new Paint();
  32. m_Paint.setAntiAlias(true);
  33. m_Paint.setDither(true);
  34. m_Paint.setColor(Color.parseColor("#37A1D1"));
  35. m_Paint.setStyle(Paint.Style.STROKE);
  36. m_Paint.setStrokeJoin(Paint.Join.ROUND);
  37. m_Paint.setStrokeCap(Paint.Cap.ROUND);
  38. m_Paint.setStrokeWidth(2);
  39.  
  40. m_Path = new Path();
  41. }
  42.  
  43. @Override
  44. protected void onSizeChanged(int w, int h, int oldw, int oldh)
  45. {
  46. super.onSizeChanged(w, h, oldw, oldh);
  47.  
  48. bitmapToCanvas = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  49. m_Canvas = new Canvas(bitmapToCanvas);
  50. }
  51.  
  52. @Override
  53. protected void onDraw(Canvas canvas)
  54. {
  55. canvas.drawBitmap(bitmapToCanvas, 0f, 0f, null);
  56. for (Pair<Path, Paint> p : arrayListPaths)
  57. {
  58. canvas.drawPath(p.first, p.second);
  59. }
  60. canvas.drawPath(m_Path, m_Paint);
  61. }
  62.  
  63. public boolean onTouch(View arg0, MotionEvent event)
  64. {
  65. float x = event.getX();
  66. float y = event.getY();
  67.  
  68. switch (event.getAction())
  69. {
  70. case MotionEvent.ACTION_DOWN:
  71. touch_start(x, y);
  72. invalidate();
  73. break;
  74. case MotionEvent.ACTION_MOVE:
  75. {
  76. touch_move(x, y);
  77. invalidate();
  78. break;
  79. }
  80. case MotionEvent.ACTION_UP:
  81. touch_up();
  82. invalidate();
  83. break;
  84. }
  85. return true;
  86. }
  87.  
  88. private void touch_start(float x, float y)
  89. {
  90. m_Path.reset();
  91. m_Path.moveTo(x, y);
  92. mX = x;
  93. mY = y;
  94. }
  95.  
  96. private void touch_move(float x, float y)
  97. {
  98. float dx = Math.abs(x - mX);
  99. float dy = Math.abs(y - mY);
  100. if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
  101. {
  102. m_Path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
  103. mX = x;
  104. mY = y;
  105. }
  106. }
  107. private void touch_up()
  108. {
  109. m_Path.lineTo(mX, mY);
  110.  
  111. // commit the path to our offscreen
  112. m_Canvas.drawPath(m_Path, m_Paint);
  113.  
  114. // kill this so we don't double draw
  115. Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
  116. arrayListPaths.add(new Pair<Path, Paint>(m_Path, newPaint));
  117. m_Path = new Path();
  118. }
  119.  
  120. public void onClickUndo ()
  121. {
  122. if (arrayListPaths.size()>0)
  123. {
  124. undonePaths.add(arrayListPaths.remove(arrayListPaths.size()-1));
  125. invalidate();
  126. }
  127. else
  128. {
  129.  
  130. }
  131. }
  132.  
  133. public void onClickRedo ()
  134. {
  135. if (undonePaths.size()>0)
  136. {
  137. arrayListPaths.add(undonePaths.remove(undonePaths.size()-1));
  138. invalidate();
  139. }
  140. else
  141. {
  142.  
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement