android bitmapfun.zip,Loading Large Bitmaps Efficiently | Android中文API

Images come in all shapes and sizes. In many cases they are larger than required for a typical

application user interface (UI). For example, the system Gallery application displays photos taken

using your Android devices's camera which are typically much higher resolution than the screen

density of your device.

Given that you are working with limited memory, ideally you only want to load a lower resolution

version in memory. The lower resolution version should match the size of the UI component that

displays it. An image with a higher resolution does not provide any visible benefit, but still takes

up precious memory and incurs additional performance overhead due to additional on the fly

scaling.

This lesson walks you through decoding large bitmaps without exceeding the per application

memory limit by loading a smaller subsampled version in memory.

Read Bitmap Dimensions and Type

The OutOfMemory

exception. Each type of decode method has additional signatures that let you specify decoding

options via the true while decoding

avoids memory allocation, returning null for the bitmap object but setting

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;

To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before

decoding it, unless you absolutely trust the source to provide you with predictably sized image data

that comfortably fits within the available memory.

Load a Scaled Down Version into Memory

Now that the image dimensions are known, they can be used to decide if the full image should be

loaded into memory or if a subsampled version should be loaded instead. Here are some factors to

consider:

Estimated memory usage of loading the full image in memory.

Amount of memory you are willing to commit to loading this image given any other memory

requirements of your application.

Dimensions of the target

Screen size and density of the current device.

For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be

displayed in a 128x96 pixel thumbnail in an

To tell the decoder to subsample the image, loading a smaller version into memory, set true in your

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) {

if (width > height) {

inSampleSize = Math.round((float)height / (float)reqHeight);

} else {

inSampleSize = Math.round((float)width / (float)reqWidth);

}

}

return inSampleSize;

}

Note: Using powers of 2 for

To use this method, first decode with true, pass the options

through and then decode again using the new 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();

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);

}

This method makes it easy to load a bitmap of arbitrarily large size into an

mImageView.setImageBitmap(

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

You can follow a similar process to decode bitmaps from other sources, by substituting the

appropriate

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值