Loading Large Bitmaps Efficiently
来源:互联网 发布:旅游策划师 知乎 编辑:程序博客网 时间:2024/05/02 22:33
在有些情况下,图片本身的大小超过了应用的需求,这时可以降低图片分辨率以降低内存消耗;
BitmapFactory提供了很多种方法用于创建bitmap(decodeByteArray()
, decodeFile()
,decodeResource()
, etc),这些方法都可以通过设置 BitmapFactory.Options
来设定解析选项;
一,获取原图片的大小
BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(getResources(), R.id.myimage, options);int imageHeight = options.outHeight;int imageWidth = options.outWidth;String imageType = options.outMimeType;
二,设置图片的sample
public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize;}
三,创建所需要的bitmap
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options);}
0 0
- Loading Large Bitmaps Efficiently
- Loading Large Bitmaps Efficiently
- Loading Large Bitmaps Efficiently
- Loading Large Bitmaps Efficiently
- Displaying Bitmaps Efficiently -Loading Large Bitmaps Efficiently
- Displaying Bitmaps Efficiently(1)-Loading Large Bitmaps Efficiently
- Displaying Bitmaps Efficiently (一)-----Loading Large Bitmaps Efficiently
- Displaying Bitmaps Efficiently (一)-----Loading Large Bitmaps Efficiently(二)
- Displaying Bitmaps Efficiently之Loading Large Bitmaps Efficiently
- Android Bitmap之Loading Large Bitmaps Efficiently
- Loading Large Bitmaps Efficiently(官方文档)
- [Developer Android] Loading Large Bitmaps Efficiently
- Loading Large Bitmaps Efficiently 高效地加载大位图
- 有效地加载大位图文件-Loading Large Bitmaps Efficiently
- Loading Large Bitmaps Efficiently 有效的处理较大的位图
- Loading Large Bitmaps Efficiently(高效加载大尺寸位图)
- 2 Loading Large Bitmaps Efficiently(高效加载大尺寸位图)
- Loading Large Bitmaps Efficiently-高效的加载大位图
- Mysql中的注释
- 立场声明--“没有丑女人只有懒女人;没有牛X的人,勤奋的你会比他更牛X”
- 【微积分】CodeForces 185B Mushroom Scientists
- 使用corosync+pacemaker构建高可用集群(转)
- intent传递对象——序列化
- Loading Large Bitmaps Efficiently
- 点滴积累
- 快餐生活时代
- Android通过POST方式传递数据到服务器简单demo
- bootstrap兼容IE6、7、8版本(IE9以下版本)
- 自测4. Have Fun with Numbers
- SAP NOTE 691154 DATA_TRANSFORM和 HIER_TRANSFORM 的说明
- C++:关于C++的RTTI (运行阶段类型识别)
- openCV学习笔记(7):cvRectangle与cv::rectangle的用法