借鉴旧帆布 - 机器人 [英] Draw From Old Canvas - Android

查看:167
本文介绍了借鉴旧帆布 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做,需要能够利用新的图形上的最后一组之上的应用程序。

这是我目前的OnDraw()方法 -

 保护无效的OnDraw(帆布油画){

    canvas.drawColor(Color.WHITE);

    如果(points.size()大于0){
        //做一些东西在这里 - 这是所有工作确定
        canvas.drawLine(p1.x,p1.y,p2.x,p2.y,linePaint);
    }
}
 

基本上,我需要绘制新的图形作为一个层上的最后的顶部,所以我正在寻找一种方法来最后一个画布的图像进行到当前。

我曾尝试使用canvas.setBitmap()方法来找出自己,但它的行为很可笑。

任何帮助AP preciated:)

PS,如果它需要的话,该类扩展 SurfaceView 和农具 SurfaceHolder.Callback

编辑:这是我已经试过了的OnDraw()方法,但它只是强制关闭

 如果(位图!= NULL){
        canvas.drawBitmap(位图,0,0,油漆);
        canvas.setBitmap(位);
    }
 

解决方案

找到自己的答案:)

  @覆盖
保护无效的OnDraw(帆布C){

如果(位= NULL和放大器;!&安培;!帆布= NULL){
    canvas.drawLine(p1.x,p1.y,p2.x,p2.y,linePaint);
    c.drawBitmap(位图,0,0,linePaint);
}
}

@覆盖
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,高度INT){

位图= Bitmap.createBitmap(宽度,高度,Config.RGB_565);
帆布=新的Canvas(位);
canvas.drawColor(Color.WHITE);
}
 

工程完全按照预期,它创建于旧画布之上绘制的效果持续

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

解决方案

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

这篇关于借鉴旧帆布 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆