Android 将view 转换为Bitmap出现空指针问题解决办法
2017-07-31 08:58
761 查看
Android 将view 转换为Bitmap出现空指针问题解决办法
在做Android 项目的时候,有时候可能有这样的需求,将一个View 或者一个布局文件转换成一个Bitmap 对象。
方法其实大都差不多。但这其中有一些小细节需要注意一下。最近在项目中用到了这个功能,现在分享一下,希望能帮助到遇到果这个
问题的人。
首先是转换 的代码:
/** * 将View(布局) 转换为bitmap * @param view * @return */ public static Bitmap createBitmap(View view){ view.setDrawingCacheEnabled(true); /** * 这里要注意,在用View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) * 来测量view 的时候,(如果你的布局中包含有 RelativeLayout )API 为17 或者 低于17 会包空指针异常 * 解决方法: * 1 布局中不要包含RelativeLayout * 2 用 View.MeasureSpec.makeMeasureSpec(256, View.MeasureSpec.EXACTLY) 好像也可以 * */ view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; }
上面就是转换成Bitmap 的方法,但是要注意,在用View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
来测量view 的时候,(如果你的布局中包含有 RelativeLayout )API 为17 或者 低于17 会包空指针异常。在项目中遇到这个问题
死活不知道是怎么回事,后来在看源码的时候才发现。以下是这个方法的官方解释:
/** * Creates a measure specification based on the supplied size and mode. * * The mode must always be one of the following: * <ul> * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li> * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li> * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li> * </ul> * * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's * implementation was such that the order of arguments did not matter * and overflow in either value could impact the resulting MeasureSpec. * {@link android.widget.RelativeLayout} was affected by this bug. * Apps targeting API levels greater than 17 will get the fixed, more strict * behavior.</p> * * @param size the size of the measure specification * @param mode the mode of the measure specification * @return the measure specification based on size and mode */ public static int makeMeasureSpec(int size, int mode) { if (sUseBrokenMakeMeasureSpec) { return size + mode; } else { return (size & ~MODE_MASK) | (mode & MODE_MASK); } }
在API 17 以上的系统中才修正了这个bug,这里有两个解决方法:
1 ,布局文件中不要包含Relativelayout 布局
2,用 View.MeasureSpec.makeMeasureSpec(256, View.MeasureSpec.EXACTLY) 好像也可以
以上就是Android 将view 转换为Bitmap出现空指针问题解决办法,如有疑问请留言或者到本站社区交流讨论,谢谢大家对本站的支持!
您可能感兴趣的文章:
相关文章推荐
- Android ListView 和ScroolView 出现onmeasure空指针的解决办法
- int android.view.View.mViewFlags NUll指针的问题解决办法
- Android之通过VideoView控件播放一个视频出现的问题以及我的解决办法
- Android 将view 转换为Bitmap ,加载布局文件空指针问题
- Android WebView中使用loadData时出现的乱码问题解决办法
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- 升级到Android 4.2.2之后adb出现device offline的问题及解决办法
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- 关于AutoCompleteTextView 为什么必须输入两个字符才能出现补全提示的问题的解决办法
- android开发出现R cannot be resolved to a variable 问题解决办法之一
- 关于android编程遇到java.lang.OutOfMemoryError: bitmap size exceeds VM budget此类问题的原因及解决办法
- Android之View转换为Bitmap及getDrawingCache=null的解决方法
- 安装AndroidStudio出现问题 JDK安装后 没有tools.jar 和dt.jar包的解决办法
- Android中 在显示ImageView时图片上面和下面都出现一段空白区间的解决办法
- 【游戏开发备注之一】关于Cocos2dx_v1.x版本iphone与Android运行出现图片白块、添加GameCenter报错问题及编译Android闪退的解决办法&&CCLOG与CClog的区别
- 如何解决Android WebView出现的乱码问题
- Android中 在显示ImageView时图片上面和下面都出现一段空白区间的解决办法
- 使用WebView加载数据出现找不到网页的问题之解决办法(转)