Unable to get the bitmap of a linearlayout along with its child views

天大地大妈咪最大 提交于 2020-01-17 08:07:25

问题


I am creating the bitmap from a linearlayout with the following code

public static Bitmap loadBitmapFromView(View v ) {

    //Bitmap b = Bitmap.createBitmap( v.getLayoutParams.width, v.getLayoutParams.height, Bitmap.Config.ARGB_8888);
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams.width, v.getLayoutParams.height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    v.draw(c);
    return b;
}

but i can not see the views in it all i can see is a big grey screen

have tried the answer at this question also Android Problems Converting ViewGroup with Children into Bitmap but still not working

My view is being inflated from xml and when i use the code mentioned above i get force close for null pointer exception and if i mention the width and height to some number like 200 , 200 then i am able to see the grey background only not the view in it.


回答1:


You need to measure the view before layout like

v.measure(
      MeasureSpec.makeMeasureSpec(v.getLayoutParams().width,MeasureSpec.EXACTLY),
      MeasureSpec.makeMeasureSpec(v.getLayoutParams().height, MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight()
        ,Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);
v.draw(c);


来源:https://stackoverflow.com/questions/8064352/unable-to-get-the-bitmap-of-a-linearlayout-along-with-its-child-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!