从旧画布中绘制 - Android

发布于 2024-09-29 10:14:31 字数 732 浏览 3 评论 0原文

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

这是我当前的 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);  
    }

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 -

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.

Any help appreciated :)

P.S if it's needed, the the class extends SurfaceView and implements SurfaceHolder.Callback

Edit: This is what I have tried in the onDraw() method but it just force closes

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

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

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

发布评论

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

评论(2

誰認得朕 2024-10-06 10:14:31

我自己找到了答案:)

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

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

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

﹎☆浅夏丿初晴 2024-10-06 10:14:31

您必须将前一个图像持久存储到 ArrayList 中,并在 ondraw 期间循环遍历 ArrayList 以重绘所有项目。

像这样的东西:

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

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