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

前提与解释:html

      安卓平台做为一款移动端的应用操做平台,其内存容量是十分有限的,内存资源是十分珍贵的,是没法与传统的桌面平台相比的,所以,在安卓平台下一样的图片操做与处理都要十分谨慎,不然你的程序能够迅速地消耗可用内存的预算,最终因为OutOfMemory致使程序崩溃掉。如下有三个缘由说明了咱们为何要谨慎:java

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

可能会上调这一限制,可是做为应用来讲必定要控制本身的内存用量,这并非能够无限制使用的。ios


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

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

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


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

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


解决办法:图片

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

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


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

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

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;

 


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

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的倍数,这样处理更快,效率更高。。。


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

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中了

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


固然,你也能够用来加载显示其余来源的图片,而不是例子中资源文件中的,下一讲咱们研究ListView,GridView中的多图片并发显示问题。

转载自移动微技

相关文章
相关标签/搜索