Android View.getDrawingCache返回null,仅null

发布于 2024-08-23 05:06:15 字数 479 浏览 5 评论 0原文

有人请尝试向我解释为什么

public void addView(View child) {
  child.setDrawingCacheEnabled(true);
  child.setWillNotCacheDrawing(false);
  child.setWillNotDraw(false);
  child.buildDrawingCache();
  if(child.getDrawingCache() == null) { //TODO Make this work!
    Log.w("View", "View child's drawing cache is null");
  }
  setImageBitmap(child.getDrawingCache()); //TODO MAKE THIS WORK!!!
}

总是记录绘图缓存为空并将位图设置为空吗?

在设置缓存之前我是否必须实际绘制视图?

谢谢!

Would anyone please try to explain to me why

public void addView(View child) {
  child.setDrawingCacheEnabled(true);
  child.setWillNotCacheDrawing(false);
  child.setWillNotDraw(false);
  child.buildDrawingCache();
  if(child.getDrawingCache() == null) { //TODO Make this work!
    Log.w("View", "View child's drawing cache is null");
  }
  setImageBitmap(child.getDrawingCache()); //TODO MAKE THIS WORK!!!
}

ALWAYS logs that the drawing cache is null, and sets the bitmap to null?

Do I have to actually draw the view before the cache is set?

Thanks!

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

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

发布评论

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

评论(10

眼眸 2024-08-30 05:06:15

我也遇到了这个问题并找到了答案:

v.setDrawingCacheEnabled(true);

// this is the important code :)  
// Without it the view will have a dimension of 0,0 and the bitmap will be null          
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache

I was having this problem also and found this answer:

v.setDrawingCacheEnabled(true);

// this is the important code :)  
// Without it the view will have a dimension of 0,0 and the bitmap will be null          
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache
樱花坊 2024-08-30 05:06:15

如果 getDrawingCache 总是返回 null 伙计们:
使用这个:

public static Bitmap loadBitmapFromView(View v) {
     Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
     Canvas c = new Canvas(b);
     v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
     v.draw(c);
     return b;
}

参考:https://stackoverflow.com/a/6272951/371749

if getDrawingCache is always returning null guys:
use this:

public static Bitmap loadBitmapFromView(View v) {
     Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
     Canvas c = new Canvas(b);
     v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
     v.draw(c);
     return b;
}

Reference: https://stackoverflow.com/a/6272951/371749

好菇凉咱不稀罕他 2024-08-30 05:06:15

得到空值的基本原因是视图没有被提及。那么,使用 view.getWidth()、view.getLayoutParams().width 等(包括 view.getDrawingCache() 和 view.buildDrawingCache())的所有尝试都是无用的。
因此,您需要首先设置视图的尺寸,例如:(

view.layout(0, 0, width, height);

您已经根据需要设置了“宽度”和“高度”或通过 WindowManager 等获取了它们)

The basic reason one gets nulls is that the view is not dimentioned. All attempts then, using view.getWidth(), view.getLayoutParams().width, etc., including view.getDrawingCache() and view.buildDrawingCache(), are useless.
So, you need first to set dimensions to the view, e.g.:

view.layout(0, 0, width, height);

(You have already set 'width' and 'height' as you like or obtained them with WindowManager, etc.)

剩一世无双 2024-08-30 05:06:15

我用这个代替。

myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Bitmap bitmap = Bitmap.createBitmap(myView.getDrawingCache());
        }
    });

Im using this instead.

myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Bitmap bitmap = Bitmap.createBitmap(myView.getDrawingCache());
        }
    });
少年亿悲伤 2024-08-30 05:06:15

工作最好 4me

    Bitmap bitmap = Bitmap.createBitmap( screen.getMeasuredWidth(), screen.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    screen.layout(0, 0, screen.getMeasuredWidth(), screen.getMeasuredHeight());
    screen.draw(canvas);

Work best 4me

    Bitmap bitmap = Bitmap.createBitmap( screen.getMeasuredWidth(), screen.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    screen.layout(0, 0, screen.getMeasuredWidth(), screen.getMeasuredHeight());
    screen.draw(canvas);
始于初秋 2024-08-30 05:06:15

如果你想要捕获的视图确实显示在屏幕上,但它返回 null。这意味着您可以在窗口管理器生成视图之前捕获视图。有些布局非常复杂。如果布局包含嵌套布局、layout_weight等,则会导致多次重新布局才能获得准确的大小。最好的解决方案是等待窗口管理器完成工作,然后获取屏幕截图。尝试将 getDrawingCache() 放入处理程序中。

If the view you want catch really shows on screen, but it return null. That means you catch the view before Window manager generate it. Some layouts are very complicated. If layout includes nested layouts, layout_weight..etc, it causes several times relayout to get exactly size. The best solution is waiting until window manager finish job and then get screen shot. Try to put getDrawingCache() in handler.

得不到的就毁灭 2024-08-30 05:06:15

@cV2 和@nininho 的答案都对我有用。

我发现的另一个原因是,我所拥有的视图正在生成一个宽度和高度为 0 的视图(即想象有一个带有空字符串的字符串文本的 TextView)。在这种情况下,getDrawingCache 将返回 null,因此请务必检查这一点。希望能帮助一些人。

@cV2 and @nininho's answers both work for me.

One of the other reasons that i found out the hard way was that the view that i had was generating a view that has width and height of 0 (i.e. imagine having a TextView with a string text of an empty string). In this case, getDrawingCache will return null, so just be sure to check for that. Hope that helps some people out there.

寂寞陪衬 2024-08-30 05:06:15

该错误可能是因为您的视图太大,无法放入绘图缓存

我从日志中得到了“View.getDrawingCache 返回 null”问题的解释:

W/View: View too large to fit into drawing cache, needs 19324704 bytes, only 16384000 available

Android 文档还 Android 设备的单个应用程序可用内存只有 16MB。这就是为什么您无法加载大位图。

The error may be bacause your View too large to fit into drawing cache.

I got the explanation of my "View.getDrawingCache returns null" problem from my logs:

W/View: View too large to fit into drawing cache, needs 19324704 bytes, only 16384000 available

Android docs also says: Android devices can have as little as 16MB of memory available to a single application. That is why you can not load big bitmap.

黎夕旧梦 2024-08-30 05:06:15

我正在尝试根据视图动态创建 n 个图像。

LayoutInflater infalter=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addview=infalter.inflate(R.layout.barcode_image_list_row_item, null);
final ImageView imv=(ImageView) addview.findViewById(R.id.imageView1);
final TextView tv=(TextView) addview.findViewById(R.id.textView1);

try {         
    final Bitmap bitmap = encodeAsBitmap(""+value, BarcodeFormat.CODE_128, 600, 300);

    if (bitmap != null) {
        // TODO Auto-generated method stub
        imv.setImageBitmap(bitmap);
        tv.setText(value);

        addview.setDrawingCacheEnabled(true);

        // this is the important code :)  
        // Without it the view will have a dimension of 0,0 and the bitmap will be null          
        addview.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        addview.layout(0, 0, addview.getMeasuredWidth(), addview.getMeasuredHeight()); 

        addview.buildDrawingCache(true);
        Bitmap b = Bitmap.createBitmap(addview.getDrawingCache());
        addview.setDrawingCacheEnabled(false); // clear drawing cache
        // saving the bitmap   
        savebarcode(b,value);
    }
} catch (WriterException e) {
    e.printStackTrace();
}

我认为这段代码会对某人有所帮助..

I am trying to creating the n number of images dynamically based on view.

LayoutInflater infalter=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addview=infalter.inflate(R.layout.barcode_image_list_row_item, null);
final ImageView imv=(ImageView) addview.findViewById(R.id.imageView1);
final TextView tv=(TextView) addview.findViewById(R.id.textView1);

try {         
    final Bitmap bitmap = encodeAsBitmap(""+value, BarcodeFormat.CODE_128, 600, 300);

    if (bitmap != null) {
        // TODO Auto-generated method stub
        imv.setImageBitmap(bitmap);
        tv.setText(value);

        addview.setDrawingCacheEnabled(true);

        // this is the important code :)  
        // Without it the view will have a dimension of 0,0 and the bitmap will be null          
        addview.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        addview.layout(0, 0, addview.getMeasuredWidth(), addview.getMeasuredHeight()); 

        addview.buildDrawingCache(true);
        Bitmap b = Bitmap.createBitmap(addview.getDrawingCache());
        addview.setDrawingCacheEnabled(false); // clear drawing cache
        // saving the bitmap   
        savebarcode(b,value);
    }
} catch (WriterException e) {
    e.printStackTrace();
}

I think this code will help someone..

李白 2024-08-30 05:06:15

这是获取位图的简单有效的方法

public void addView(View v) {
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache();

        Bitmap bitmap = v.getDrawingCache();

        if(bitmap == null) {
            // bitmap is null
            // do whatever you want
        } else {
            setImageBitmap(bitmap);
        }
        v.setDrawingCacheEnabled(false);
        v.destroyDrawingCache();
    }

This the simple and efficient way to get bitmap

public void addView(View v) {
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache();

        Bitmap bitmap = v.getDrawingCache();

        if(bitmap == null) {
            // bitmap is null
            // do whatever you want
        } else {
            setImageBitmap(bitmap);
        }
        v.setDrawingCacheEnabled(false);
        v.destroyDrawingCache();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文