高效的显示位图(二):高效加载大…
来源:互联网 发布:安庆历史气象数据查询 编辑:程序博客网 时间:2024/05/21 09:44
- 图像经常比应用UI需要的大
* 指定解码选项 *inJustDecodeBound:只取尺寸、类型信息,不分配内存给图像数据 * 这项技术允许你读取尺寸和类型,而免于真正的构建位图:
- 理想的,值加载低分辨率版本到内存中
- 低分辨率版本应当与显示它的界面元素匹配
- 本篇讲解如何不超过应用内存限制解码大图片,途径是加载更小的二次采样版本
-----------------------------------------------------------------------------------------
读取位图尺寸和类型
- BitmapFactory类提供了多个解码方法来从不同的源创建位图对象
- 容易OOM
- 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;
- 为了避免OOM,要在解码位图之前检查尺寸,除非你确信没有问题
-----------------------------------------------------------------------------------------
加载缩小版本到内存
- 已经获取的尺寸信息可以用来决定是加载原始图像还是加载二次采样的图像
- 应当考虑:
- 估计加载原始图像的内存使用情况
- 你希望用于图像加载的内存数量
- 目标ImageView或UI组件的尺寸
- 屏幕的尺寸和密度
-为告知解码器进行二次采样,在BitmapFactory.Options中将inSampleSize(倍数)设置为true
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) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
= heightRatio < widthRatio ? heightRatio : widthRatio; inSampleSize
}
return inSampleSize;
}
- inSampleSize设为2能让解码器更快更有效。但如果二次采样的图像用于缓存,用最合适的倍率仍然是值得的
- inJustDecodeBounds设置回false
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();
.inJustDecodeBounds = true; options
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options
// Decode bitmap with inSampleSize set
.inJustDecodeBounds = false; options
return BitmapFactory.decodeResource(res, resId, options);
}
- 此方法很容易的将大尺寸图像变成100*100版本加载到ImageView:
mImageView.setImageBitmap(
(getResources(), R.id.myimage, 100, 100)); decodeSampledBitmapFromR esource
- 高效的显示位图(二):高效加载大…
- 高效加载大位图
- 高效的加载大Bitmap(位图)
- Android---高效的加载大位图
- Android---高效的加载大位图
- 高效的显示位图(五):管理位图内…
- 连载 1 - 深入讨论 Android 关于高效显示图片的问题 - 如何高效的加载大位图
- 高效地显示Bitmap图片 1 - 有效率地加载大尺寸的位图
- 创建带有动画的APP 之 高效的显示位图:高效的载入大的位图
- 高效的显示位图(一):概述
- Android Developers:高效的加载大的位图
- Loading Large Bitmaps Efficiently-高效的加载大位图
- Loading Large Bitmaps Efficiently 高效的加载大尺寸位图
- 高效地加载大Bitmap(位图)
- 高效地加载大Bitmap(位图)
- Android官方开发文档Training系列课程中文版:高效显示位图之加载大位图
- 高效的显示位图(四):缓冲位图
- 【Android Training - 09】高效地显示Bitmap图片 [ Lesson 1 - 有效率地加载大尺寸的位图]
- Android的系统服务
- 定位与传感器:定位和地图
- 应用程序组件:应用程序基础
- Android文档学习:Task
- 高效的显示位图(一):概述
- 高效的显示位图(二):高效加载大…
- 素数查找——另一种方法
- 高效的显示位图(四):缓冲位图
- 高效的显示位图(五):管理位图内…
- 高效的显示位图(六):在UI上显示…
- debug
- 使用JavaScript控制当前文本输入框高亮显示
- CentOS6.4配置SVN服务
- myeclipse 编辑JS很慢的解决办法