Android Problems Converting ViewGroup with Children into Bitmap

后端 未结 2 1643
臣服心动
臣服心动 2020-12-24 09:36

I\'m successfully converting a ViewGroup (RelativeLayout) into Bitmap using a Canvas. However, when the draw happens I only see the ViewGroup with its background drawable an

相关标签:
2条回答
  • 2020-12-24 09:59

    Solution that based on DisplayMetrics for the Layouts could be useful as well. In order not to repeat - look here .

    0 讨论(0)
  • 2020-12-24 10:17

    The problem is that you did not measure and layout the container. You must call v.measure(widthSpec, heightSpec) and then v.layout(left, top, right, bottom) before drawing can work. The first method will make sure the view knows how big you want it to be, and the second method will ensure the children are positioned properly.

    In your case you would do:

    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);
    
    0 讨论(0)
提交回复
热议问题