0

我正在像这样(如下)在 WebView 上创建画布。我正在实现一个撤消功能,因此用户可以通过单击按钮来删除绘制的路径。但是,即使我离开活动并返回,画布上也总是填充有我的路径。删除它的唯一方法是关闭应用程序。如何删除或重新创建位图canvasBitmap或画布drawCanvas

public static Canvas drawCanvas;
public static Bitmap canvasBitmap;
Rect clipBounds;

public static ArrayList<Path> paths = new ArrayList<Path>();
public static ArrayList<Paint> colors = new ArrayList<Paint>();

// Creates bitmap for canvas
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
}

// Saves the paths onto the bitmap and adjust scale based on current zoom level
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    clipBounds = canvas.getClipBounds();
    drawPaint.setStrokeWidth(8/mScaleFactor);
    canvas.scale(mScaleFactor, mScaleFactor, 0, 0);

    for (int i = 0; i < colors.size(); i++) {
        canvas.drawPath(paths.get(i), colors.get(i));
    }
    canvas.drawPath(drawPath, drawPaint);
    canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
    invalidate();
}

public boolean onTouchEvent(MotionEvent event) {
    pointerCount = event.getPointerCount();
    mScaleDetector.onTouchEvent(event);
    float touchX = (event.getX() + clipBounds.left) / mScaleFactor;
    float touchY = (event.getY() + clipBounds.top) / mScaleFactor;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                drawPath.moveTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_MOVE:
                drawPath.lineTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_UP:
                 Paint newPaint = new Paint();
                newPaint.set(drawPaint); //copies the values over from the current class's paint
                colors.add(newPaint);
                paths.add(drawPath);
                drawPath = new Path();
                drawPath.reset();
                break;
            default:
                return false;
        }
    }
    invalidate();
    return true;
}

public void reDrawUndo () {
     if (paths.size() > 0) {
         paths.remove(paths.size() - 1);
         colors.remove(colors.size() - 1);
         invalidate();
     }
}

我正在使用此方法将具有相应颜色的路径绘制到画布上:

for (int j = 0; j < paths.size(); j++) {
    drawCanvas.drawPath(paths.get(j),colors.get(j));
           }
4

1 回答 1

0

您保留在“路径”列表中的所有点,并且您持有当前绘制的路径“drawPath”:这是您的“逻辑”。

当你想在屏幕上看到它们时,你可以在onDraw(Canvas canvas)提供的画布上绘制它们:这是你在 UI 上绘制的逻辑。

当您想保存积分时,您可以选择的选项很少,这取决于您想要实现的目标。当您想保存一个位图并在其上绘制路径时 - 您创建一个新位图(您不必一直将其保存在内存中)和一个画布,然后在新画布上绘制所有点。当您想保存点以便以后绘制它们时,只需将它们保存为文件或数据库中的整数或文本。

于 2013-11-06T16:04:38.383 回答