关于Android Paint绘图颜色

前端之家收集整理的这篇文章主要介绍了关于Android Paint绘图颜色前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
DrawView. java
  1. public class DrawView extends View implements OnTouchListener {
  2. private Canvas mCanvas;
  3. private Path mPath;
  4. public Paint mPaint;
  5. ArrayList<Path> paths = new ArrayList<Path>();
  6. private ArrayList<Path> undonePaths = new ArrayList<Path>();
  7.  
  8. private MaskFilter mEmboss;
  9. private MaskFilter mBlur;
  10.  
  11. private Bitmap im;
  12.  
  13. public DrawView(Context context) {
  14. super(context);
  15. setFocusable(true);
  16. setFocusableInTouchMode(true);
  17. this.setOnTouchListener(this);
  18.  
  19. paths.clear();
  20. undonePaths.clear();
  21.  
  22. mPaint = new Paint();
  23. mPaint.setAntiAlias(true);
  24. mPaint.setDither(true);
  25. mPaint.setColor(Color.BLUE);
  26. mPaint.setStyle(Paint.Style.STROKE);
  27. mPaint.setStrokeJoin(Paint.Join.ROUND);
  28. mPaint.setStrokeCap(Paint.Cap.ROUND);
  29. mPaint.setStrokeWidth(4);
  30. mEmboss = new EmbossMaskFilter(new float[] { 1,1,1 },0.4f,6,3.5f);
  31.  
  32. mBlur = new BlurMaskFilter(8,BlurMaskFilter.Blur.NORMAL);
  33.  
  34. mCanvas = new Canvas();
  35. mPath = new Path();
  36. // paths.add(mPath);
  37.  
  38. im = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);
  39.  
  40. }
  41.  
  42. @Override
  43. protected void onSizeChanged(int w,int h,int oldw,int oldh) {
  44. super.onSizeChanged(w,h,oldw,oldh);
  45. }
  46.  
  47. @Override
  48. protected void onDraw(Canvas canvas) {
  49. canvas.drawColor(Share.cColor);
  50.  
  51. for (Path p : paths) {
  52.  
  53. canvas.drawPath(p,mPaint);
  54. }
  55.  
  56. canvas.drawPath(mPath,mPaint);
  57.  
  58. }
  59.  
  60. private float mX,mY;
  61. private static final float TOUCH_TOLERANCE = 4;
  62.  
  63. private void touch_start(float x,float y) {
  64.  
  65. mPaint.setColor(Share.dColor);
  66. undonePaths.clear();
  67. mPath.reset();
  68. mPath.moveTo(x,y);
  69. mX = x;
  70. mY = y;
  71.  
  72. Log.e("","pathsize:::" + paths.size());
  73. Log.e("","undonepathsize:::" + undonePaths.size());
  74. }
  75.  
  76. private void touch_move(float x,float y) {
  77. float dx = Math.abs(x - mX);
  78. float dy = Math.abs(y - mY);
  79. if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
  80. mPath.quadTo(mX,mY,(x + mX) / 2,(y + mY) / 2);
  81. mX = x;
  82. mY = y;
  83. }
  84. }
  85.  
  86. private void touch_up() {
  87. mPath.lineTo(mX,mY);
  88. // commit the path to our offscreen
  89. mCanvas.drawPath(mPath,mPaint);
  90. // kill this so we don't double draw
  91. paths.add(mPath);
  92. mPath = new Path();
  93.  
  94. Log.e("","undonepathsize:::" + undonePaths.size());
  95. }
  96.  
  97. public void onClickUndo() {
  98.  
  99. Log.e("","undonepathsize:::" + undonePaths.size());
  100. if (paths.size() > 0) {
  101. undonePaths.add(paths.remove(paths.size() - 1));
  102. invalidate();
  103. } else {
  104.  
  105. }
  106. // toast the user
  107. }
  108.  
  109. public void onClickRedo() {
  110.  
  111. Log.e("","undonepathsize:::" + undonePaths.size());
  112. if (undonePaths.size() > 0) {
  113. paths.add(undonePaths.remove(undonePaths.size() - 1));
  114. invalidate();
  115. } else {
  116.  
  117. }
  118. // toast the user
  119. }
  120.  
  121. @Override
  122. public boolean onTouch(View arg0,MotionEvent event) {
  123. float x = event.getX();
  124. float y = event.getY();
  125.  
  126. switch (event.getAction()) {
  127. case MotionEvent.ACTION_DOWN:
  128. touch_start(x,y);
  129. invalidate();
  130. break;
  131. case MotionEvent.ACTION_MOVE:
  132. touch_move(x,y);
  133. invalidate();
  134. break;
  135. case MotionEvent.ACTION_UP:
  136. touch_up();
  137. invalidate();
  138. break;
  139. }
  140. return true;
  141. }

}

我试图绘制不同颜色的手指画,但每当我改变颜色的油漆然后所有以前的路径或绘图得到和绘制更新的颜色我想用不同的颜色绘制我该怎么办?请给我一些解决方案.

解决方法

为此,您必须为绘制的每个对象创建一个新的Paint.这是因为当Canvas重绘时,它每次引用相同的Paint对象,因此所有路径都将使用此绘制.

首先,我会更改您的路径数组以包含Paint和Path.您可以使用Android类型对来实现此目的.

  1. ArrayList<Pair<Path,Paint>> paths = new ArrayList<Pair<Path,Paint>>();

您还必须以这种方式转换您的undonePaths变量.

然后,在touch_up()方法中,您需要添加这个新的Paint对象.

  1. Paint newPaint = new Paint(mPaint); // Clones the mPaint object
  2. paths.add(new Pair<Path,Paint>(mPath,newPaint));

最后,你的循环也必须为此调整:

  1. for (Pair<Path,Paint> p : paths) {
  2. canvas.drawPath(p.first,p.second);
  3. }

这是非常耗费内存的,所以当它们不再使用时你必须小心重置这些项目,但是要拥有这么多不同的颜色,你必须拥有所有这些不同的Paint对象.

猜你在找的Android相关文章