同步手绘板——将View的内容映射成Bitmap转图片导出

时间:2023-03-09 19:25:18
同步手绘板——将View的内容映射成Bitmap转图片导出

在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap,但是刚开始使用的时候,得到的结果都是null,所以在一个论坛里查到了正确的使用方法.代码如下:

contentLayout.setDrawingCacheEnabled(true);    

        contentLayout.measure(    

               MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),    

                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));    

       contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),    

                contentLayout.getMeasuredHeight());    

     contentLayout.buildDrawingCache();    

      Bitmap bitmap= contentLayout.getDrawingCache();   

在使用的时候调用

Bitmap bitmap = view.getDrawingCache();

就可以得到图片的bitmap了。