【问题标题】:Android View.getDrawingCache returns null, only nullAndroid View.getDrawingCache 返回null,只有null
【发布时间】:2010-02-26 04:06:27
【问题描述】:

谁能解释一下原因

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

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

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

谢谢!

【问题讨论】:

标签: android android-view


【解决方案1】:

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

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

【讨论】:

  • WebView 测量取决于内容大小,因此如果网页尚未加载,您将获得 0 的测量值。尝试传递实际尺寸来测量,而不是像 MeasureSpec.makeMeasureSpec(800, MeasureSpec.EXACTLY);
  • 即使使用 MeasureSpec.EXACTLY 仍然返回 null。
  • 从足够大的角度来看,这对我不起作用。 (可能是因为我的手机内存资源非常有限)。 cV2 下面的答案对我来说很好。
  • @nininho 如何在不使用 getdrawingchache() 方法的情况下从 imageview 获取可绘制对象?
  • 效果很好。但它会影响 UI。请帮忙解决这个问题。
【解决方案2】:

如果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

【讨论】:

  • 好一个。这个对我有用,而其他的没有。是否有任何理由使用来自getLayoutParams 的值而不是 v.getWidth() 和 v.getHeight()?
  • 抱歉,就个人而言,无法帮助您解决您的问题 :'( -> 很高兴 :)
  • @cV2 我得到了图像上的绘图,但我需要在运行 surfaceview 时获取 getdrawingcache .. 那么接下来我该怎么办?
  • 我得到异常宽度和高度必须> 0 ......................私有void takeScreenShot() { for (int i = 1; i
  • 我认为当您的ImageView 尚未收到图像时,会出现`异常宽度和高度必须> 0`,因此它似乎为0。这对于不同的人来说会有所不同代码,但请确保您对loadBitmapFromView() 的调用绝对是在您的ImageView 包含图像之后。
【解决方案3】:

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

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

(你已经设置了'width'和'height',或者通过WindowManager等获取它们)

【讨论】:

  • 查看根目录 = currActivity.getWindow().getDecorView().findViewById(android.R.id.content); root.setDrawingCacheEnabled(true); root.layout(0, 0, 480, 854); mBitmap = root.getDrawingCache();
【解决方案4】:

我用这个代替。

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

【讨论】:

    【解决方案5】:

    最好的工作 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);
    

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

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

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

        【讨论】:

          【解决方案8】:

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

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

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

          Android 文档也有 saysAndroid 设备可以为单个应用程序提供低至 16MB 的内存。这就是为什么你不能加载大位图的原因。

          【讨论】:

            【解决方案9】:

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

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

            【讨论】:

              【解决方案10】:

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-11-26
                • 1970-01-01
                • 2021-04-19
                • 1970-01-01
                • 2012-06-22
                • 2012-05-28
                • 2014-12-05
                • 2021-07-07
                相关资源
                最近更新 更多