android ImageView.getDrawingCache return NULL
来源:互联网 发布:大数据研发工程师笔试 编辑:程序博客网 时间:2024/06/05 08:42
情况如: http://www.oschina.net/question/6530_44340
模拟器上面getDrawingCache可以返回值,而在真机上面返回null
当我们需要获取某个View的DrawingCache时回去调用getDrawingCache方法,有同学说可以使用getDrawable;但这两个是不一样的,根本上的不一样暂时还没去研究,至少getDrawable获取的是原图(至少大小是跟原图一样大),而getDrawingCache返回的Image是ImageView控件显示的一个截屏
那么当getDrawingCache返回null时我们怎么解决
http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null,这篇blog说添加如下代码:
// 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());
后面我发现,如果是你的DrawingCache超过最大Cache,这招管用,如果不是,这招不管用。
现在网上很多遇到的getDrawingCache返回为null都是DrawingCache大小超过Cache大小导致的,但我这里不是这个问题导致。
我看了下View源码,getDrawingCache是这样实现的:
public Bitmap getDrawingCache() {
return getDrawingCache(false);
}
我们接着看getDrawingCache(false):
public Bitmap getDrawingCache(boolean autoScale) {
if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
return null;
}
if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
buildDrawingCache(autoScale);
}
return autoScale ? (mDrawingCache == null ? null : mDrawingCache.get()) :
(mUnscaledDrawingCache == null ? null : mUnscaledDrawingCache.get());
}
如果getDrawingCache返回null,大体两种可能
1) (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING 这个值为true
2) (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED 为false,buildDrawingCache没执行
3) buildDrawingCache执行失败
我们怎么去粗略的估计是那种原因导致的呢
后面我发现View的willNotCacheDrawing这个方法,代码如下
public boolean willNotCacheDrawing() {
return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
}
他返回的就是刚才的第一种情况的那个boolean值,我们可以直接debug查看是否为true,如果该值为true,可以强行转成false,View.setWillNotCacheDrawing( Boolean.FALSE ).
第二种情况可以看View的 isDrawingCacheEnabled 方法
public boolean isDrawingCacheEnabled() {
return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
}
如果这个方法为false,那就是之前忘记调用setDrawingCacheEnabled(Boolean.TRUE)方法了。
至于第三种,zartzwj『http://www.iteye.com/topic/1098839』同学有分析,
在buildDrawingCache(boolean)方法中有这样一段代码
if (width <= 0 || height <= 0 ||
// Projected bitmap size in bytes
(width * height * (opaque && !translucentWindow ? 2 : 4) >
ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
destroyDrawingCache();
return;
}
很多同学遇到的可能都是这种, (width * height * (opaque && !translucentWindow ? 2 : 4) 大于 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize()导致buildDrawingCache失败。
怎么判断是否这里导致的呢,如果你在前面没有对View的layout自行设置,是程序默认,并且调用的是view.getDrawingCache()或者是view.getDrawingCache(false);
view.getHidth的值就是这儿的width值
view.getHeight的值就是这儿的height
将这两个值乘以4如果还没大于ViewConfiguration.get(Context).getScaledMaximumDrawingCacheSize(),那么就不可能是是DrawingCache大于最大Cache所导致的,
如果乘以4大于了,也不一定就是,因为(width * height * (opaque && !translucentWindow ? 2 : 4)有这个变数。
我遇到的是第一种情况,willNotCacheDrawing我没有调用,使用的默认值,但为什么在模拟器上面为false,而在真机上就是true了呢?现在还不是很清楚
刚接触android不久,对API不是很熟,各位大牛轻拍
- android ImageView.getDrawingCache return NULL
- 【android学习】imageview通过getDrawingCache获取图像
- 对View进行截图 View.getDrawingCache return NULL
- Android:getDrawingCache() = null的解决方法
- android截屏报getDrawingCache()==null
- Android DrawingCache超詳細解析,解決getDrawingCache方法回傳null
- Android:解决view.getDrawingCache()返回null的问题
- Android DrawingCache超詳細解析,解決getDrawingCache方法回傳null
- Android:解决view.getDrawingCache()返回null的问题
- getDrawingCache()=null的解决办法
- Android开发:用getDrawingCache方法获取ImageView中的图像需要注意的问题
- 用getDrawingCache方法获取ImageView中的图像
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- Android之View转换为Bitmap及getDrawingCache=null的解决方法
- Android之View转换为Bitmap及getDrawingCache=null的解决方法
- js实现年月日三级下拉菜单联动
- 使用递归的快速排序算法
- quartz 详解
- javascript高级部分
- Tomcat6.0配置(虚拟目录的设置+多域名绑定)
- android ImageView.getDrawingCache return NULL
- android 面试
- web架构设计经验分享
- 对单元测试的思考
- Readability付费计划失败带给创业者的反思
- 内部类模拟实现多继承
- C#三层结构
- 如何把批处理文件放到启动项中
- Win32 多线程的创建方法和基本使用