版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010838785/article/details/90069798
/**
* 作者:guoyzh
* 时间:2019/4/12 16:06
* 功能:手写画笔
*/
public class CustomerPainterView extends View {
private Paint mPaint;
private Path mPath;
private float mLastX;
private float mLastY;
private Canvas canvas;
public CustomerPainterView(Context context) {
this(context, (AttributeSet) null);
}
public CustomerPainterView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomerPainterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.initView();
}
private void initView() {
this.mPath = new Path();
this.mPaint = new Paint();
this.mPaint.setStyle(Paint.Style.STROKE);
this.mPaint.setColor(Color.parseColor("#303F9F"));
this.mPaint.setStrokeWidth(5.0F);
this.mPaint.setAntiAlias(true);
// getParent().requestDisallowInterceptTouchEvent(false);
}
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.mPath.moveTo(x, y);
this.mLastX = x;
this.mLastY = y;
return true;
case MotionEvent.ACTION_MOVE:
float endX = (this.mLastX + x) / 2.0F;
float endY = (this.mLastY + y) / 2.0F;
this.mPath.quadTo(this.mLastX, this.mLastY, endX, endY);
this.mLastX = x;
this.mLastY = y;
this.invalidate();
case MotionEvent.ACTION_UP:
default:
return super.onTouchEvent(event);
}
}
protected void onDraw(Canvas canvas) {
this.canvas = canvas;
canvas.drawPath(this.mPath, this.mPaint);
}
public void setPenColorRes(@ColorRes int penColorId) {
int color = this.getResources().getColor(penColorId);
this.mPaint.setColor(color);
}
public void setPenColor(@ColorInt int penColor) {
this.mPaint.setColor(penColor);
}
public void setPenWidth(float penWidth) {
this.mPaint.setStrokeWidth(penWidth);
}
public void clear() {
this.mPath.reset();
this.invalidate();
}
public Bitmap createBitmap() {
return BitmapUtil.convertViewToBitmap(this, this.getWidth(), this.getHeight());
}
/**
* 清空当前已绘制内容
*/
public void clearDrawable() {
clear();
}
}