Android View.getDrawingCache返回null,只返回nu

有人请尝试向我解释原因

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记录绘图缓存为空,并将位图设置为null?

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

谢谢!

Bex asked 2019-08-13T02:31:15Z
10个解决方案
232 votes

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

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
Marcio Covre answered 2019-08-13T02:31:27Z
57 votes

如果getDrawingCache总是returning 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]

cV2 answered 2019-08-13T02:31:59Z
6 votes

获得null的基本原因是视图不是维数。 然后,使用view.getWidth(),view.getLayoutParams()。width等进行所有尝试,包括view.getDrawingCache()和view.buildDrawingCache(),都是无用的。因此,您首先需要为视图设置尺寸,例如:

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

(您已经设置了' width'和' height'如您所愿或使用WindowManager等获得它们)

Apostolos answered 2019-08-13T02:32:31Z
4 votes

我正在使用它。

myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Bitmap bitmap = Bitmap.createBitmap(myView.getDrawingCache());
        }
    });
Tyro answered 2019-08-13T02:32:55Z
2 votes

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

Kislingk answered 2019-08-13T02:33:20Z
2 votes

工作得最好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);
Alexey Kurilov answered 2019-08-13T02:33:45Z
1 votes

@ cV2和@nininho的答案都适合我。

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

Bundeeteddee answered 2019-08-13T02:34:17Z
1 votes

我试图根据视图动态创建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);

            }else{

            }


        } catch (WriterException e) {
            e.printStackTrace();
        }

我认为这段代码会帮助别人..

Harish answered 2019-08-13T02:34:49Z
1 votes

该错误可能是因为您的View太大而无法适应绘图缓存。

我得到了我的" View.getDrawingCache的解释返回null" 来自我的日志的问题:

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

Android文档还说:Android设备可以为单个应用程序提供少至16MB的内存。 这就是为什么你不能加载大位图。

Yuliia Ashomok answered 2019-08-13T02:35:29Z
-1 votes

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

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();
    }
B2N answered 2019-08-13T02:35:53Z
translate from https://stackoverflow.com:/questions/2339429/android-view-getdrawingcache-returns-null-only-null