首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Canvas to Bitmap

Android Canvas to Bitmap
EN

Stack Overflow用户
提问于 2012-10-21 10:46:11
回答 2查看 11.1K关注 0票数 3

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

代码语言:javascript
复制
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;





        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-21 12:39:25

试着这样..。

-使用Bitmap.createBitmap()创建大小正确的bitmap

-使用Canvas(Bitmap)构造函数创建指向此位图的canvas instance

-绘制到canvas

-使用bitmap

票数 6
EN

Stack Overflow用户

发布于 2016-07-14 22:49:24

代码语言:javascript
复制
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();}
    }
}

创建一个新实例:

代码语言:javascript
复制
CanvasToBitmap canvasToBitmap = new CanvasToBitmap(getBaseContext());

获取canvasToBitmap的位图:

代码语言:javascript
复制
Bitmap bitmap = canvasToBitmap.bitmap;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12994487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档