Android View.getDrawingCache返回null,仅返回null

时间:2010-02-26 04:06:28

标签: android android-view

有人请尝试向我解释原因吗

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

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

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

谢谢!

10 个答案:

答案 0 :(得分:232)

答案 1 :(得分:57)

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

答案 2 :(得分:6)

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

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

(您已经设置了' width'和' height'您喜欢或使用WindowManager等获取它们)

答案 3 :(得分:4)

我正在使用它。

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

答案 4 :(得分:2)

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

答案 5 :(得分:2)

最佳工作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 :(得分:1)

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

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

答案 7 :(得分:1)

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

我认为此代码可以帮助某人......

答案 8 :(得分:1)

错误可能是您的查看太大而无法适应绘图缓存

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

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

Android文档says Android设备可以为单个应用程序提供少至16MB的内存。这就是无法加载大位图的原因。

答案 9 :(得分:-1)

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

bot.dialog('/', function (session) {
    const card = new builder.HeroCard(session);
    card.title("Type your question to our support team here");
    card.subtitle("Or, click the ◀▶ arrows for a series of how-tos");
    card.images([builder.CardImage.create(session,"<image url>")])
    card.text("<p>This is some <i>mardown</i> <b>text</b>!</p> <ul><li><b>one</b></li><li><code>two</code></li></ul>")
    const msg = new builder.Message(session);
    msg.attachmentLayout(builder.AttachmentLayout.carousel);
    msg.attachments([
        card,card,card
    ]).toMessage();
    session.send(msg);
    session.endDialog("This is some *mardown* **text**! \n\n - **one** \n\n - `two`");
});