在 Android FingerPaint 演示中如何删除绘制的路径

发布于 2024-11-29 11:41:17 字数 707 浏览 0 评论 0原文

我一直在尝试更改 Android (v3.0 Honeycomb) API 演示 FingerPaint (API >= 11) 以包含一个可删除最后绘制路径的工作擦除选项。

API 演示中的擦除效果不佳,在 v3.0 中会崩溃,在 v3.1 中会绘制一个黑框,而仅部分擦除路径(已提出一个错误,不确定是否已在 v3.2 中解决[不适用于英国的 Xoom 设备])。

我的代码如下:

public void eraseLastPath() {
    if (!mPaths.isEmpty()) {
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mCanvas.drawPath(mPaths.pop(), mPaint);
        invalidate();
    }
}

这有点有效,但会在画布上留下绘制路径的轮廓。例如

这个: 在此处输入图像描述

被删除,如下所示: 在此处输入图像描述

我缺少什么?有没有更好的方法来删除路径?

非常感谢任何帮助。

谢谢

I've been trying to alter the Android (v3.0 Honeycomb) API Demo FingerPaint (API >= 11) to include a working erase option that removes the last drawn path.

The erase in the API demo doesnt work well, in v3.0 is crashes, in v3.1 is draws a black box while only partially erasing the path (a bug has been raised not sure if it has been resolved in v3.2 [not out for Xoom device in UK]).

My code is as follows:

public void eraseLastPath() {
    if (!mPaths.isEmpty()) {
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mCanvas.drawPath(mPaths.pop(), mPaint);
        invalidate();
    }
}

This somewhat works but leaves an outline of the drawn path on the canvas. e.g.

this:
enter image description here

is erased to look like this:
enter image description here

What am I missing? Is there a better way to erase paths?

Any help is much appreciated.

Thanks

Joe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-12-06 11:41:17

尝试将 mPaint 抗锯齿设置为打开。

编辑:

擦除路径的正确方法是将所有笔划存储到 ArrayList 中,然后删除不需要的笔划并重新绘制所有内容。问题中使用的方法并不是真正删除路径,而更像是在其上绘制另一种颜色,但是抗锯齿的工作方式与使用第一种颜色的方式不同。

每个路径都需要是新类“笔划”中的成员对象,该类还存储了颜色、绘画、滤镜等。通过这种方式,所有绘图都可以恢复,并且可以无限撤消。

Try to set mPaint anti-alias on.

Edit:

The proper way to erase a path is to store all strokes into an ArrayList and then remove the unwanted one and redraw everything. The method used in the question is not really deleting a path but more like drawing another colour over it, but then anti-alias would not work the same as with the first colour.

Each path needs to be a member object in a new class "stroke", which also has colour, paint, filters etc stored. In this way all drawing can be restored and it can have infinite undo.

红颜悴 2024-12-06 11:41:17

单独保存最后绘制的Path(不绘制到Bitmap上)进行绘制。这允许您在将路径提交到支持位图之前对其进行操作。

例如:

private List<Path> undoablePaths;    

@Override
protected void onDraw(Canvas canvas) {
    for(Path path : undoablePaths){
        canvas.drawPath(path, pathPaint);   
    }
    canvas.drawBitmap(mainBitmap, 0, 0, bitmapPaint);
}

private void undoLast(){
    undoablePaths.remove(undoablePaths.size() - 1);
    invalidate();
}

注意:该示例不允许使用多种颜色。您只需扩展 Path 对象并在其上保存颜色信息(并相应地设置 Paint)。

另外,如果您将其与 Honeycomb 设备一起使用,请确保使用 android:hardwareAccelerated="true" 打开硬件加速。

Hold the last drawn Path(s) separately (not drawn onto Bitmap) for drawing. This allows you to manipulate the Paths before committing them to a backing Bitmap.

For example:

private List<Path> undoablePaths;    

@Override
protected void onDraw(Canvas canvas) {
    for(Path path : undoablePaths){
        canvas.drawPath(path, pathPaint);   
    }
    canvas.drawBitmap(mainBitmap, 0, 0, bitmapPaint);
}

private void undoLast(){
    undoablePaths.remove(undoablePaths.size() - 1);
    invalidate();
}

Note: The example doesn't allow for multiple colours. You can just extend the Path object and hold colour information on it (and set the Paint accordingly).

Also, if you are using this with a Honeycomb device, make sure you have hardware acceleration turned on with android:hardwareAccelerated="true".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文