当前位置:   article > 正文

Android 实现涂鸦笔效果_android paint 蜡笔效果

android paint 蜡笔效果

1、主要可以通过手指绘制各种图案、还可以清除回撤等效果,实现比较简单。

 

2、主要实现代码,功能实现比较简单。

  1. package com.example.mytestproject;
  2. import android.content.Context;
  3. import android.graphics.BlurMaskFilter;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Matrix;
  7. import android.graphics.Paint;
  8. import android.graphics.drawable.Drawable;
  9. import android.util.AttributeSet;
  10. import android.util.Log;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. import androidx.annotation.Nullable;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. public class DrawView extends View {
  17. public float mCurrentX = 40;
  18. public float mCurrentY = 50;
  19. private float mDownX;
  20. private float mDownY;
  21. private Paint mPaint;
  22. private Line mCurrentLine = new Line();
  23. private ArrayList<Line> lines = new ArrayList<>();
  24. private float mLastMoveX = 0;
  25. private float mLastMoveY = 0;
  26. private int mSpace;
  27. private Matrix mMatrix;
  28. private Drawable[] mDrawables = new Drawable[3];
  29. private List<Line> mCheLines = new ArrayList<>();
  30. private int mResetPosition = 0;
  31. public DrawView(Context context) {
  32. this(context, null);
  33. }
  34. public DrawView(Context context, @Nullable AttributeSet attrs) {
  35. this(context, attrs, 0);
  36. }
  37. public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  38. super(context, attrs, defStyleAttr);
  39. // mPaint.setShader(new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.MIRROR));
  40. // BlurMaskFilter maskFilter = new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL);
  41. // mPaint.setMaskFilter(maskFilter);
  42. //Shader shader0 = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
  43. // Shader shader = new LinearGradient(0, 0, 30, 0, new int[]{Color.WHITE, Color.RED, Color.RED}, new float[]{0f, 0.5f, 1f}, Shader.TileMode.REPEAT);
  44. // Shader shader1 = new RadialGradient(10, 10, 10, new int[]{Color.GREEN, Color.WHITE, Color.GREEN}, new float[]{0, 0.5f, 1}, Shader.TileMode.MIRROR);
  45. // Shader shader2 = new SweepGradient(15, 15, Color.RED, Color.BLUE);
  46. // Shader shader3 = new ComposeShader(shader, shader1, PorterDuff.Mode.MULTIPLY);
  47. // mMatrix = new Matrix();
  48. // mLinearGradient.setLocalMatrix(mMatrix);
  49. // mPaint.setShader(shader);
  50. BlurMaskFilter PaintBGBlur = new BlurMaskFilter(10, BlurMaskFilter.Blur.SOLID);
  51. mPaint = new Paint();
  52. mPaint.setStyle(Paint.Style.STROKE);
  53. mPaint.setStrokeCap(Paint.Cap.ROUND);
  54. mPaint.setMaskFilter(PaintBGBlur);
  55. //mPaint.setAntiAlias(true);
  56. mPaint.setColor(Color.RED);
  57. mDrawables[0] = getResources().getDrawable(R.mipmap.star_big_off);
  58. mDrawables[1] = getResources().getDrawable(R.mipmap.star_big_on);
  59. mDrawables[2] = getResources().getDrawable(R.mipmap.rate_star_med_on_holo_light);
  60. mSpace = mDrawables[0].getIntrinsicWidth();
  61. mPaint.setStrokeWidth(mDrawables[0].getIntrinsicWidth());
  62. }
  63. @Override
  64. protected void onDraw(Canvas canvas) {
  65. super.onDraw(canvas);
  66. for (int i = 0; i < lines.size(); i++) {
  67. drawLine(canvas, lines.get(i));
  68. }
  69. drawLine(canvas, mCurrentLine);
  70. }
  71. private void drawLine(Canvas canvas, Line line) {
  72. for (int i = 0; i < line.points.size() - 1; i++) {
  73. float x = line.points.get(i).x;
  74. float y = line.points.get(i).y;
  75. int size = mDrawables.length;
  76. int surplus = i%size;
  77. for (int j=0;j<size;j++){
  78. if (surplus==j){
  79. mDrawables[j].setBounds((int) x, (int) y, (int) x + mDrawables[j].getIntrinsicWidth(), (int) y + mDrawables[j].getIntrinsicHeight());
  80. mDrawables[j].draw(canvas);
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. @Override
  87. public boolean onTouchEvent(MotionEvent event) {
  88. switch (event.getAction()) {
  89. case MotionEvent.ACTION_DOWN:
  90. mDownX = event.getX();
  91. mDownY = event.getY();
  92. mLastMoveX = mDownX;
  93. mLastMoveY = mDownY;
  94. break;
  95. case MotionEvent.ACTION_MOVE:
  96. mCurrentX = event.getX();
  97. mCurrentY = event.getY();
  98. if (Math.abs(mCurrentX - mLastMoveX) > mSpace || Math.abs(mCurrentY - mLastMoveY) > mSpace) {
  99. ViewPoint point = new ViewPoint();
  100. point.x = mCurrentX;
  101. point.y = mCurrentY;
  102. mCurrentLine.points.add(point);
  103. mLastMoveX = mCurrentX;
  104. mLastMoveY = mCurrentY;
  105. }
  106. break;
  107. case MotionEvent.ACTION_UP:
  108. lines.add(mCurrentLine);
  109. mCheLines.add(mCurrentLine);
  110. mCurrentLine = new Line();
  111. mResetPosition = mCheLines.size();
  112. break;
  113. }
  114. invalidate();
  115. return true;
  116. }
  117. public void last() {
  118. mResetPosition--;
  119. if (mResetPosition <= 0) {
  120. mResetPosition = 0;
  121. }
  122. lines.clear();
  123. for (int i = 0; i < mResetPosition; i++) {
  124. lines.add(mCheLines.get(i));
  125. }
  126. invalidate();
  127. }
  128. public void next() {
  129. mResetPosition++;
  130. if (mResetPosition >= mCheLines.size()) {
  131. mResetPosition = mCheLines.size();
  132. }
  133. lines.clear();
  134. for (int i = 0; i < mResetPosition; i++) {
  135. lines.add(mCheLines.get(i));
  136. }
  137. invalidate();
  138. }
  139. public void clear() {
  140. mCheLines.clear();
  141. lines.clear();
  142. mCurrentLine.points.clear();
  143. invalidate();
  144. }
  145. }

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/598708
推荐阅读
相关标签
  

闽ICP备14008679号