Android画布转为位图

3

我一直在网上搜索如何将画布上的内容转换成位图。我尝试了多种方法,比如将绘制缓存保存到位图中,但最终结果是背景位图会闪烁一下,然后变成黑屏。测试图像显示在背景上,但不会被下一次调用 OnDraw 的背景覆盖。

MainThreading{

                        ...

              if(notuptodate == true){

                        //call readyBackground to create the background bitmap
                        this.mainPanel.readyBackground(canvas);
                        //post the canvas
                        surfaceHolder.unlockCanvasAndPost(canvas);
                        //clean the Canvas
                        canvas = null;
                        //ready new Canvas
                        canvas = this.surfaceHolder.lockCanvas();//lock the canvas to enable editing






                    }//if not true

                    if (MainThreading.notuptodate == false){

                        mainPanel.onDraw(canvas);


                    }//if false
...
                 }//mainthreading

    //this method is run first to create the Bitmap that on draw will use
    public void readyBackGround(Canvas canvas){


        if (MainThreading.notuptodate){
            //method used to draw multiple Bitmaps onto the canvas
            renderBackground(canvas);

            //private Bitmap, this is the supposed proper size of the bitmap
            backgroundBitmap=Bitmap.createBitmap(240, 320, Config.ARGB_8888);


            //set the canvas to turn whats on its self onto the bitmap.
            canvas.setBitmap(backgroundBitmap);

            //set boolean flag to false so renderBackground won't be called
            //untill it needs to be updated again
            MainThreading.notUpToDate = false;

        }

        //this method is called when notUpToDate = true
         @Override
    protected void onDraw(Canvas canvas){


        if (this.threado.getBackground() != null){
            canvas.drawBitmap(backgroundBitmap, 0, 0, null);

            }

            //this if statement is never activated
            if (this.threado.getBackground() == null){
                Log.d("background nonexistant", "Importante!");
            }
            //draw test object onto background
            test.DrawObject(canvas);

                //when MainThreading.notUpToDate = true isn't commented out, the
                //screen consistantly shows the background, but the test object isn't
                //drawn

        //MainThreading.notUpToDate = true;





        }
2个回答

6
请按如下方式尝试:

- 使用Bitmap.createBitmap()创建正确大小的位图。

- 使用Canvas(Bitmap)构造函数创建指向该位图的画布实例。

- 在画布上绘制。

- 使用该bitmap


起初,这似乎没有解决我的问题,但我找到了导致这个问题的错误。在OnDraw方法中,您会看到一些if语句引用了一些旧代码,我忘记删除它们了。感谢您的答复,非常感谢。 - Andrew Raappana
5
将Canvas转换为位图图像在Android中的实现:
  1. 首先创建一个Bitmap对象,并通过其createBitmap()方法指定宽度和高度。
  2. 然后创建一个Canvas对象,并传入位图对象作为参数。
  3. 在Canvas对象上绘制需要转换的内容,例如视图或绘图形状。
  4. 最后,调用Bitmap对象的compress()方法将其压缩到输出流中,以生成位图图像。
以下是示例代码:// 创建位图对象 Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 创建画布对象并绑定到位图上 Canvas canvas = new Canvas(bm); // 在画布上绘制需要转换的内容 view.draw(canvas); // 将位图压缩到输出流上(这里使用了ByteArrayOutputStream作为输出流) ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] byteArray = baos.toByteArray();以上代码将视图view绘制在指定宽度和高度的位图上,并将其压缩为PNG格式的字节数组。可以根据需要改变压缩格式和质量值。
- AlexAndro

1
public class CanvasToBitmap extends View {

    Paint paint = new Paint();
    Rect mRect = new Rect();
    Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);

    public myCanvas( Context context ) {
        super(context);
        Canvas canvas = new Canvas(bitmap);
        draw(canvas);
    }

    @Override
    public void onDraw(Canvas canvas) {
        
        mRect.set(0, 0, 200, 200);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(mRect, paint);
        canvas.setBitmap(bitmap);

        ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
        try{
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, mByteArrayOutputStream);

            bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(mByteArrayOutputStream.toByteArray()));
            mByteArrayOutputStream.close();
       } catch (Exception e) {e.printStackTrace();}
    }
}

创建一个新实例:

CanvasToBitmap canvasToBitmap = new CanvasToBitmap(getBaseContext());

获取canvasToBitmap的位图:
Bitmap bitmap = canvasToBitmap.bitmap;

图片大小为200x200。 - amiron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接