1

我正在制作一个需要能够在最后一组之上绘制新图形的应用程序。

这是我当前的 onDraw() 方法 -

protected void onDraw(Canvas canvas) {

    canvas.drawColor(Color.WHITE);

    if(points.size() > 0) {
        //do some stuff here - this is all working ok
        canvas.drawLine(p1.x, p1.y, p2.x, p2.y, linePaint);
    }   
}

基本上,我需要将新图形绘制为最后一个图层之上的一层,所以我正在寻找一种将最后一个画布的图像带到当前的方法。

我尝试使用 canvas.setBitmap() 方法自己弄清楚它,但它的行为非常有趣。

任何帮助表示赞赏:)

PS如果需要,该类扩展SurfaceView并实现SurfaceHolder.Callback

编辑:这是我在 onDraw() 方法中尝试过的,但它只是强制关闭

if(bitmap != null) {
        canvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.setBitmap(bitmap);  
    }
4

2 回答 2

4

自己找到了答案:)

@Override
protected void onDraw(Canvas c) {

if(bitmap != null && canvas != null) { 
    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, linePaint);
    c.drawBitmap(bitmap, 0, 0, linePaint);  
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
}

完全按预期工作,它创造了在旧画布上连续绘图的效果

于 2010-10-29T16:09:24.677 回答
0

You will have to store the previous image persistently onto a ArrayList, and during ondraw, loop through the ArrayList to redraw all items.

something like this:

for (Graphic graphic : _graphics) {
    bitmap = graphic.getBitmap();
    coords = graphic.getCoordinates();
    canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
}
于 2010-10-28T03:29:01.373 回答