无法获取线性布局的位图及其子视图

发布于 2024-12-14 08:40:42 字数 787 浏览 1 评论 0原文

我使用以下代码从线性布局创建位图

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;
}

但我看不到其中的视图 我所看到的只是一个大灰色屏幕

也尝试了这个问题的答案 Android 将带有子项的 ViewGroup 转换为位图时出现问题 但仍然无法正常工作

我的视图正在从 xml 中膨胀 当我使用上面提到的代码时,我会强制关闭空指针异常 如果我提到像 200 、 200 这样的数字的宽度和高度,那么我只能看到灰色背景,而看不到其中的视图。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-12-21 08:40:42

您需要在布局之前测量视图,例如

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文