Android 如何擦除画布上绘制的线条?
我正在使用Android 如何擦除画布上绘制的线条?,android,canvas,draw,erase,Android,Canvas,Draw,Erase,我正在使用Canvas类在画布上绘制线条。现在,我想用和我们在笔记本上用橡皮擦一样的方法擦除画布上画的线。我列举了几个例子,但没有一个对我有用 如果有人知道这个问题的解决办法,你能帮我解决吗 Java代码: public class DrawView extends View implements OnTouchListener { private Canvas m_Canvas; private Path m_Path
Canvas
类在画布上绘制线条。现在,我想用和我们在笔记本上用橡皮擦一样的方法擦除画布上画的线。我列举了几个例子,但没有一个对我有用
如果有人知道这个问题的解决办法,你能帮我解决吗
Java代码:
public class DrawView extends View implements OnTouchListener
{
private Canvas m_Canvas;
private Path m_Path;
private Paint m_Paint;
ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>();
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private Bitmap bitmapToCanvas;
private CanvasManager m_CanvasManagerObject;
private Paint mBitmapPaint;
public DrawView(Context context)
{
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
onCanvasInitialization();
}
public void onCanvasInitialization()
{
m_Paint = new Paint(Paint.DITHER_FLAG);
m_Paint.setAntiAlias(true);
m_Paint.setDither(true);
m_Paint.setColor(Color.parseColor("#37A1D1"));
m_Paint.setStyle(Paint.Style.STROKE);
m_Paint.setStrokeJoin(Paint.Join.ROUND);
m_Paint.setStrokeCap(Paint.Cap.ROUND);
m_Paint.setStrokeWidth(2);
m_Path = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
bitmapToCanvas = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
m_Canvas = new Canvas(bitmapToCanvas);
}
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmapToCanvas, 0f, 0f, mBitmapPaint);
canvas.drawPath(m_Path, m_Paint);
}
public boolean onTouch(View arg0, MotionEvent event)
{
float x = event.getX();
float y = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
private void touch_start(float x, float y)
{
m_Path.reset();
m_Path.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y)
{
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
{
m_Path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up()
{
m_Path.lineTo(mX, mY);
// commit the path to our offscreen
m_Canvas.drawPath(m_Path, m_Paint);
// kill this so we don't double draw
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
m_Path = new Path();
}
public void onClickEraser()
{
}
}
公共类DrawView扩展视图实现OnTouchListener
{
私人帆布m_帆布;
私有路径m_路径;
私人涂料m_涂料;
ArrayList路径=新的ArrayList();
ArrayList undonePaths=新建ArrayList();
私人浮动mX,我的;
专用静态最终浮动接触公差=4;
私有位图bitmapToCanvas;
私人画布经理m_CanvasManagerObject;
私人油漆;
公共绘图视图(上下文)
{
超级(上下文);
设置聚焦(真);
setFocusableInTouchMode(真);
this.setOnTouchListener(this);
oncanvas初始化();
}
public void oncanvas初始化()
{
m_油漆=新油漆(油漆抖动_标志);
m_Paint.setAntiAlias(真);
m_Paint.setDither(真);
m#u Paint.setColor(Color.parseColor(#37A1D1”);
m_Paint.setStyle(Paint.Style.STROKE);
m_油漆。设置行程连接(油漆。连接。圆形);
m_油漆固定行程盖(油漆盖圆形);
m_油漆设置行程宽度(2);
m_Path=新路径();
mBitmapPaint=新油漆(油漆抖动标志);
}
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh)
{
super.onSizeChanged(w,h,oldw,oldh);
bitmapToCanvas=Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
m_Canvas=新画布(bitmapToCanvas);
}
@凌驾
受保护的void onDraw(画布)
{
画布.drawBitmap(bitmapToCanvas、0f、0f、MbitMapPoint);
canvas.drawPath(m_Path,m_Paint);
}
公共布尔onTouch(视图arg0,运动事件)
{
float x=event.getX();
float y=event.getY();
开关(event.getAction())
{
case MotionEvent.ACTION\u DOWN:
触摸启动(x,y);
使无效();
打破
case MotionEvent.ACTION\u移动:
触摸移动(x,y);
使无效();
打破
case MotionEvent.ACTION\u UP:
润色;
使无效();
打破
}
返回true;
}
专用无效触摸启动(浮动x、浮动y)
{
m_Path.reset();
m_路径移动到(x,y);
mX=x;
mY=y;
}
私有无效触摸移动(浮动x、浮动y)
{
float dx=Math.abs(x-mX);
float dy=Math.abs(y-mY);
如果(dx>=接触公差| | dy>=接触公差)
{
m_路径四分图(mX,mY,(x+mX)/2,(y+mY)/2);
mX=x;
mY=y;
}
}
私人空间修补()
{
m_Path.lineTo(mX,mY);
//将路径提交到我们的屏幕外
m_Canvas.drawPath(m_Path,m_Paint);
//杀了它,这样我们就不会重复抽签了
Paint newPaint=new Paint(m_Paint);//克隆mPaint对象
添加(新对(m_Path,newPaint));
m_Path=新路径();
}
公共无效OnClick橡皮擦()
{
}
}
为了擦除,除了笔刷颜色外,还需要设置背景颜色。
请想象您正在使用mspaint,橡皮擦本身正在“绘制”canva上的背景色
如果你的背景是000
那么,画笔可能是这样的
delPaint = new Paint();
delPaint.setColor(0x00000000);
delPaint.setXfermode(clear);
delPaint.setAlpha(0x00);
delPaint.setAntiAlias(true);
delPaint.setDither(true);
delPaint.setStyle(Paint.Style.STROKE);
delPaint.setStrokeJoin(Paint.Join.ROUND);
delPaint.setStrokeCap(Paint.Cap.ROUND);
如果您有纯色背景,则只需将
Paint
color设置为您的背景色。例如,如果您有白色背景,则可以执行以下操作:
paint.setColor(Color.White);
但是,如果需要删除具有透明背景的线条,请尝试以下操作:
为了使用透明颜色进行绘制,必须使用Paint
setXfermode
,只有在画布上设置位图时,该功能才起作用。如果您按照以下步骤操作,您应该会得到所需的结果
mCanvas = new Canvas();
mBitmap= Bitmap.createBitmap(scrw, scrh, Config.ARGB_8888);
mCanvas.setBitmap(mBitmap);
public void onClickEraser()
{
if (isEraserOn)
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
else
mPaint.setXfermode(null);
}
mCanvas.drawPath(路径,mPaint)代码>
与Daniel Albert的回答一致, 使用后:
public void onClickEraser()
{
if (isEraserOn)
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
else
mPaint.setXfermode(null);
}
…您应该提交touch\u move
方法上的所有绘制,以避免实体路径。如果isErase=true
relativeLayout=(relativeLayout)findViewById(R.id.relativelayout1),则也禁用drawpath
;
relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout1);
button = (Button)findViewById(R.id.button);
view = new SketchSheetView(slate.this);
paint = new Paint();
path2 = new Path();
relativeLayout.addView(view, new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
paint.setDither(true);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(5);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
path2.reset();
relativeLayout.removeAllViewsInLayout();
view = new SketchSheetView(slate.this);
relativeLayout.addView(view, new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
}
});
}
private class SketchSheetView extends View {
public SketchSheetView(slate slate) {
super(slate);
bitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_4444);
canvas = new Canvas(bitmap);
this.setBackgroundColor(Color.WHITE);
}
ArrayList<DrawingClass> DrawingClassArrayList= new ArrayList<DrawingClass>();
@Override
public boolean onTouchEvent(MotionEvent event) {
DrawingClass pathWithPaint = new DrawingClass();
canvas.drawPath(path2, paint);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
path2.moveTo(event.getX(), event.getY());
path2.lineTo(event.getX(), event.getY());
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
path2.lineTo(event.getX(), event.getY());
pathWithPaint.setPath(path2);
pathWithPaint.setPaint(paint);
DrawingClassArrayList.add(pathWithPaint);
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (DrawingClassArrayList.size() > 0) {
canvas.drawPath(
DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPath(),
DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPaint());
}
}
}
public class DrawingClass {
Path DrawingClassPath;
Paint DrawingClassPaint;
public Path getPath() {
return DrawingClassPath;
}
public void setPath(Path path) {
this.DrawingClassPath = path;
}
public Paint getPaint() {
return DrawingClassPaint;
}
public void setPaint(Paint paint) {
this.DrawingClassPaint = paint;
}
}
按钮=(按钮)findViewById(R.id.button);
视图=新的SketchSheetView(slate.this);
油漆=新油漆();
路径2=新路径();
relativeLayout.addView(视图,新relativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_父级,
RelativeLayout.LayoutParams.MATCH_PARENT));
绘制。设置抖动(真);
油漆。设置颜色(颜色。黑色);
绘制.设置样式(绘制.样式.笔划);
绘制.设置行程连接(绘制.连接.圆形);
油漆固定行程盖(油漆固定行程盖圆形);
油漆。设置行程宽度(5);
按钮