微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android Canvas到Bitmap

我一直在网上搜索如何将画布上的内容转换为位图.我尝试了多种方法来实现这一点,例如将绘图缓存保存到位图,但最终结果是背景位图闪烁了一会儿,然后变成了黑屏.测试图像显示在背景上,但不会被下次调用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;





        }

解决方法:

试试这种方式……

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

– 使用Canvas(位图)构造函数创建指向此位图的画布实例

– 画到画布上

– 使用位图

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐