[英]Eraser effect using canvas
I have a custom view class which extends the view. 我有一个自定义视图类,它扩展了视图。 I am using the over-ridden onDraw method to doodle on the canvas.
我正在使用过度使用的onDraw方法在画布上进行涂鸦。 I have a list of a POJO which includes Path and Paint with the help of which I can re-create the old paths drawn, something like this:
我有一个包含Path和Paint的POJO列表,借助它我可以重新创建绘制的旧路径,如下所示:
@Override
protected void onDraw(Canvas canvas){
for (POJO pojo : pojoList {
canvas.drawPath(pojo.path, pojo.paint);
}
canvas.drawPath(path, paint);
}
I want to implement erase function which should work like a proper erase. 我想实现擦除功能,它应该像正确擦除一样工作。 I know one way to do it to write to a bitmap and use setXfermode to CLEAR which properly works.
我知道一种方法来写一个位图并使用setXfermode来正确工作的CLEAR。 But pixelation doesn't look good on smaller resolution devices.
但是在较小分辨率的设备上像素化效果不佳。 I don't want to use WHITE color paint as underneath this view I have an ImageView, so coloring it white will also effect the ImageView as well.
我不想在这个视图下面使用WHITE颜色颜色我有一个ImageView,所以将它着色也会影响ImageView。 Any idea or snippet towards solving this problem is highly appreciated.
我们非常感谢您解决此问题的任何想法或代码。
This works just fine. 这很好用。 If someone finds a better solution.
如果有人找到更好的解决方案。 Kindly update
请更新
private Context mContext;
private ArrayList<Draw> mDrawList;
private Paint mPaint;
private Path mPath;
private boolean isErase;
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
public EraseDoodleView(Context context) {
super(context);
init(context);
}
public EraseDoodleView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init(context);
}
private void init(Context context) {
mContext = context;
mDrawList = new ArrayList<Draw>();
mPath = new Path();
setupPaint();
setLayerType(View.LAYER_TYPE_HARDWARE, mPaint);
}
private void setupPaint() {
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setFilterBitmap(true);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(4);
mPaint.setColor(ContextCompat.getColor(mContext, android.R.color.black));
if (isErase) {
mPaint.setXfermode(new PorterDuffXfermode(
PorterDuff.Mode.CLEAR));
mPaint.setStrokeWidth(16);
}
}
public void setErase(boolean isErase) {
this.isErase = isErase;
setupPaint();
}
public boolean getErase() {
return this.isErase;
}
@Override
protected void onDraw(Canvas canvas) {
for (Draw draw : mDrawList) {
canvas.drawPath(draw.path, draw.paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp();
invalidate();
break;
}
return true;
}
private void touchStart(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mDrawList.add(new Draw(mPath, mPaint));
mX = x;
mY = y;
}
private void touchMove(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touchUp() {
mPath.lineTo(mX, mY);
mPath = new Path();
}
class Draw {
Path path;
Paint paint;
public Draw(Path path, Paint paint) {
this.paint = paint;
this.path = path;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.