开发者

In Android FingerPaint demo how can I erase drawn paths

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 p开发者_开发知识库ath.

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:

In Android FingerPaint demo how can I erase drawn paths

is erased to look like this:

In Android FingerPaint demo how can I erase drawn paths

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

Any help is much appreciated.

Thanks

Joe


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.


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜