Android高效显示图片详解(一) .

http://blog.csdn.net/zhiying201039/article/details/8653786

说明:

      本讲义分为三部分,较为详细的介绍了Android平台下图片显示,加载等操作的处理原则与办法,以供大家共同学习,转载请注明出处 “From 移动微技”。


前提与解释:

      安卓平台作为一款移动端的应用操作平台,其内存容量是十分有限的,内存资源是十分珍贵的,是无法与传统的桌面平台相比的,因此,在安卓平台下同样的图片操作与处理都要十分谨慎,否则你的程序可以迅速地消耗可用内存的预算,最终由OutOfMemory导致程序崩溃掉。以下有三个原因说明了我们为什么要谨慎:

(1)安卓平台下对应用可使用的系统资源都做出了限制,标准安卓系统下,一个应用程序可用的最大内存为16M,一些第三方ROM

可能会上调这一限制,但是作为应用来说一定要控制自己的内存用量,这并不是可以无限制使用的。


(2)一张高分辨图片的内容耗用量是惊人的,例如,Galaxy Nexus的摄像头在拍摄2592X1936像素(5百万像素)。如果位图使用

的是配置ARGB_8888(默认的Android 2.3开始),那么此图像加载到内存占用约19MB的内存(2592 * 1936 * 4字节),直接就耗

尽了在某些设备上的每个应用程序的内存上限。


(3)安卓应用程序的一些控件经常需要几个位图一起加载。例如ListView,GridView,ViewPager等控件,并且在使用中还要快速

的滑动,要及时对图片进行更新与回收,更加增加了图片处理的难度。


解决办法:

一,如何去加载与显示大图:

          其实,在安卓这样内存有限的平台上,是没有必要按照原始尺寸把一张大图完全加载进来的,只需要加载与我们显示控件相匹配的尺寸就行,多了只会浪费我们宝贵的内存。因此在加载图片时,我们按照我们需要显示的大小对原始图片再采样就OK了。同时我们也可以根据我们所能够使用的内存大小来对图片进行解码,按照我们能够承受的尺寸与分辨率来处理,保证图片所占用的内存在我们可支配的范围之内,也就避免了OOM的问题。


第一步:我们需要获取原始图片的相关尺寸,分辨率等数据

                   可以利用BitmapFactory的Options来达到这一目的,解码图片时可以先把inJustDecodeBounds的值设为true,这样并没有真正的去解码图片,不占用内存,但是我们却可以在这个过程中获取图片的宽,高以及类型,代码如下:

  1. BitmapFactory.Options options = new BitmapFactory.Options();  
  2. options.inJustDecodeBounds = true;  
  3. BitmapFactory.decodeResource(getResources(), R.id.myimage, options);  
  4. int imageHeight = options.outHeight;  
  5. int imageWidth = options.outWidth;  
  6. String imageType = options.outMimeType;  
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;


第二步:获取原始的图片尺寸后,根据目标计算缩放比例系数,代码如下:

  1. public static int calculateInSampleSize(  
  2.             BitmapFactory.Options options, int reqWidth, int reqHeight) {  
  3.     // Raw height and width of image  
  4.     final int height = options.outHeight;  
  5.     final int width = options.outWidth;  
  6.     int inSampleSize = 1;  
  7.   
  8.     if (height > reqHeight || width > reqWidth) {  
  9.   
  10.         // Calculate ratios of height and width to requested height and width  
  11.         final int heightRatio = Math.round((float) height / (float) reqHeight);  
  12.         final int widthRatio = Math.round((float) width / (float) reqWidth);  
  13.   
  14.         // Choose the smallest ratio as inSampleSize value, this will guarantee  
  15.         // a final image with both dimensions larger than or equal to the  
  16.         // requested height and width.  
  17.         inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  18.     }  
  19.   
  20.     return inSampleSize;  
  21. }  
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.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

官方文档中说,inSampleSize这个属性最好是2的倍数,这样处理更快,效率更高。。。


第三步:开始对图片进行解码,代码如下:

  1. public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,  
  2.         int reqWidth, int reqHeight) {  
  3.   
  4.     // First decode with inJustDecodeBounds=true to check dimensions  
  5.     final BitmapFactory.Options options = new BitmapFactory.Options();  
  6.     options.inJustDecodeBounds = true;  
  7.     BitmapFactory.decodeResource(res, resId, options);  
  8.   
  9.     // Calculate inSampleSize  
  10.     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);  
  11.   
  12.     // Decode bitmap with inSampleSize set  
  13.     options.inJustDecodeBounds = false;  
  14.     return BitmapFactory.decodeResource(res, resId, options);  
  15. }  
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);
}


注意,真正解码时需要把inJustDecodeBounds属性重置为false,这样就可以把一张十分巨大的图轻松显示在一个100x100的ImageView中了

  1. mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));  
mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));


当然,你也可以用来加载显示其他来源的图片,而不是例子中资源文件中的,下一讲我们研究ListView,GridView中的多图片并发显示问题。


Android图片显示详解(二)地址:http://blog.csdn.net/zhiying201039/article/details/8665598

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值