gpt4 book ai didi

android - 如何删除在 Canvas 上绘制的线条?

转载 作者:太空狗 更新时间:2023-10-29 15:47:20 26 4
gpt4 key购买 nike

我正在使用 Canvas 类在 Canvas 上绘制线条。现在,我想以与使用橡皮擦在笔记本中类似的方式删除在 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()
{

}

}

最佳答案

如果您有纯色背景,您需要做的就是将Paint 颜色设置为您的背景色。例如,如果你有一个白色背景,你可以这样做:

paint.setColor(Color.White);

但是,如果您需要删除具有透明背景的线条,您可以试试这个:

为了使用透明颜色进行绘制,您必须使用Paint setXfermode,这仅在您将位图设置到 Canvas 时才有效。如果您按照以下步骤操作,您应该会得到想要的结果。

  1. 创建 Canvas 并设置其位图。

    mCanvas = new Canvas();
    mBitmap= Bitmap.createBitmap(scrw, scrh, Config.ARGB_8888);
    mCanvas.setBitmap(mBitmap);
  2. 当您想要删除某些内容时,您只需使用 setXfermode。

    public void onClickEraser() 
    {
    if (isEraserOn)
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    else
    mPaint.setXfermode(null);
    }
  3. 现在您应该可以使用透明颜色绘制:

    mCanvas.drawPath(path, mPaint);

关于android - 如何删除在 Canvas 上绘制的线条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15332062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com