首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android View.getDrawingCache返回null,仅返回null

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

Stack Overflow用户
提问于 2010-02-26 12:06:28
回答 8查看 73.4K关注 0票数 99

有没有人能给我解释一下为什么

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

是否始终记录图形缓存为空,并将位图设置为空?

在设置缓存之前,我真的需要绘制视图吗?

谢谢!

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2011-01-07 01:58:13

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

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
票数 245
EN

Stack Overflow用户

发布于 2012-08-13 23:25:15

如果getDrawingCache总是returning null guys:使用这个:

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

票数 61
EN

Stack Overflow用户

发布于 2016-03-13 18:21:40

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

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

(您已经根据自己的喜好设置了“width”和“height”,或者使用WindowManager等工具获取了它们。)

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2339429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档