简体   繁体   English

从旧画布中绘制 - Android

[英]Draw From Old Canvas - Android

I'm making an App that needs to be able to draw new graphics on top of the last set. 我正在制作一个需要能够在最后一组上绘制新图形的应用程序。

This is my current onDraw() method - 这是我目前的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);
    }   
}

Basically, I need to draw the new graphics as a layer on top of the last, so what I'm looking for is a way to carry the image of the last canvas to the current. 基本上,我需要将新图形作为最后一层的图层绘制,所以我正在寻找的是将最后一个画布的图像传递到当前的方法。

I have tried to figure it out myself using the canvas.setBitmap() method but it acts very funny. 我试图使用canvas.setBitmap()方法自己解决它,但它的行为非常有趣。

Any help appreciated :) 任何帮助赞赏:)

PS if it's needed, the the class extends SurfaceView and implements SurfaceHolder.Callback PS如果需要,该类扩展SurfaceView并实现SurfaceHolder.Callback

Edit: This is what I have tried in the onDraw() method but it just force closes 编辑:这是我在onDraw()方法中尝试过的,但它只是强制关闭

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

Found the answer myself :) 自己找到了答案:)

@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);
}

Works exactly as intended, it creates the effect of drawing on top of the old canvas continuously 完全按照预期工作,它创造了连续绘制旧画布顶部的效果

You will have to store the previous image persistently onto a ArrayList, and during ondraw, loop through the ArrayList to redraw all items. 您必须将前一个图像持久存储到ArrayList中,并且在ondraw期间,循环遍历ArrayList以重绘所有项目。

something like this: 这样的事情:

for (Graphic graphic : _graphics) {
    bitmap = graphic.getBitmap();
    coords = graphic.getCoordinates();
    canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM