Bitmap优化问题
来源:互联网 发布:电脑编程有几种 编辑:程序博客网 时间:2024/05/17 03:23
在Android项目中,如果直接使用ImageView显示Bitmap会占用较多的资源,如果图片过大,会造成程序崩溃。为了解决这个问题需要对Bitmap进行压缩,以节省内存。因为项目中用到,所以写下来,以备不时之需。我们可以使用BitmapFactory的options设置图片压缩比,具体代码如下:
/** * @param absolutePath 图片文件路径 * @return 压缩后的图片 */public static Bitmap bitmapReduce(String absolutePath){ Bitmap bm; BitmapFactory.Options opt = new BitmapFactory.Options(); //这个isjustdecodebounds很重要 opt.inJustDecodeBounds = true; bm = BitmapFactory.decodeFile(absolutePath, opt); //获取到这个图片的原始宽度和高度 int picWidth = opt.outWidth; int picHeight = opt.outHeight; //这个可以自己设置,也可以传参,值越小,压缩程度越大 int screenWidth = 200; int screenHeight = 200; //isSampleSize是表示对图片的缩放程度,比如值为2图片的宽度和高度都变为以前的1/2,图片大小为原来1/4 opt.inSampleSize = 1; //根据屏的大小和图片大小计算出缩放比例 if(picWidth > picHeight){ if(picWidth > screenWidth) opt.inSampleSize = picWidth/screenWidth; } else{ if(picHeight > screenHeight) opt.inSampleSize = picHeight/screenHeight; } //这次再真正地生成一个有像素的,返回经过缩放了的bitmap opt.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(absolutePath, opt); return bm; }
1 0
- Bitmap性能优化问题
- Bitmap优化问题
- 有关bitmap优化问题
- bitmap优化
- Bitmap优化
- Bitmap 优化
- Bitmap优化
- BitMap优化
- Bitmap优化
- 内存优化---Bitmap优化
- Android BitMap的优化
- android加载Bitmap优化
- Android Bitmap图像优化
- Android Bitmap图像优化
- Android Bitmap图像优化
- Android Bitmap图像优化
- Android Bitmap图像优化
- Bitmap内存优化123
- arm看门狗的作用
- HDU-3613 Best Reward (Manacher)
- 关于Math这个类的一些基本方法的案例实现
- [Offer收割]编程练习赛1 hihocoder 1269 优化延迟 (二分+优先权队列)
- LeetCode : Valid Parentheses [java]
- Bitmap优化问题
- 331. Verify Preorder Serialization of a Binary Tree
- 动态转移方程 免费馅饼
- qsort函数用法
- python小点心--获取汉子拼音首字母
- 《Java实战开发经典》第六章6.1
- nyoj 首尾相连数组的最大子数组和
- SSL编程
- <BLE>CC2540主机获取各个从机广播