Android 将view 转换为Bitmap出现空指针问题解决办法
作者:lqh 时间:2022-02-12 02:17:23
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) 好像也可以
来源:http://blog.csdn.net/zwluoyuxi/article/details/44259275
标签:Android,view,Bitmap,空指针
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java反射之通过反射了解集合泛型的本质(详解)
2023-02-04 03:10:25
![](https://img.aspxhome.com/file/2023/1/68151_0s.png)
C#使用log4net记录日志
2022-12-04 03:08:35
![](https://img.aspxhome.com/file/2023/8/106448_0s.jpg)
Androd 勇闯高阶性能优化之布局优化篇
2023-01-22 12:12:15
![](https://img.aspxhome.com/file/2023/8/87278_0s.png)
简单讲解Android开发中触摸和点击事件的相关编程方法
2023-03-30 12:29:10
全面了解Java中的内部类和匿名类
2023-01-05 09:14:23
java.lang.NoSuchMethodException: com.sun.proxy.$Proxy58.list错误解决办法
2021-05-25 03:11:19
java两个integer数据判断相等用==还是equals
2021-06-14 00:46:52
详解maven中profiles使用实现
2022-11-13 23:14:24
![](https://img.aspxhome.com/file/2023/2/64512_0s.jpg)
java元注解@Inherited的使用详解
2023-09-15 04:58:48
![](https://img.aspxhome.com/file/2023/9/119779_0s.png)
android使用gesturedetector手势识别示例分享
2023-08-08 15:06:11
c#使用listbox的详细方法和常见问题解决
2023-08-27 11:28:56
![](https://img.aspxhome.com/file/2023/9/68069_0s.jpg)
如何将Object类转换为实体类
2021-11-05 04:45:11
Java 反射机制详解及实例代码
2023-07-13 15:22:29
Springboot下RedisTemplate的两种序列化方式实例详解
2021-09-11 11:48:47
![](https://img.aspxhome.com/file/2023/6/87346_0s.png)
在Android中通过Intent使用Bundle传递对象的使用方法
2023-04-23 10:15:38
![](https://img.aspxhome.com/file/2023/4/99774_0s.jpg)
详解Guava中EventBus的使用
2021-09-13 07:36:34
![](https://img.aspxhome.com/file/2023/7/97987_0s.png)
Java使用JDBC实现Oracle用户认证的方法详解
2022-10-06 08:59:36
Android Intent实现页面跳转的两种方法
2022-03-13 22:27:10
![](https://img.aspxhome.com/file/2023/8/93288_0s.png)
Java中的几种读取properties配置文件的方式
2022-09-06 13:36:19
深入理解C++中public、protected及private用法
2023-07-02 11:30:17