Android can effectively process Bitmap and reduce memory usage.
Android can effectively process Bitmap and reduce memory usage.
The image size may vary. In many cases, the image size will exceed the size we need. For example, in our Camera application, the size of the photos we take is much larger than that displayed on the screen.
If your application is limited to memory usage, it is obvious that you will choose to load a low-resolution image. This low-resolution image should match the screen size. Higher-resolution images do not offer any visible benefits, but still occupy valuable memory, and additional performance overhead due to additional Dynamic Scaling.
This article teaches you to modify a large image by loading a small image sample without exceeding the memory limit of the application.
Original address http://wear.techbrood.com/training/displaying-bitmaps/load-bitmap.html
The BitmapFactory class provides several decoding methods (for example, decodeByteArray (), decodeFile (), and decodeResource (). When we load the image amount, images may come from many sources. We can decode the image using a suitable method. The following provides a method: re-allocate the memory for the created Bitmap, which can effectively solve the problem of memory overflow (OutOfMemory Exception. Each decoding method has some identifiers. You can configure BitmapFactory. Options to implement these identifiers. When the inJustDecodeBounds attribute is set to true, no memory is allocated during decoding, and the returned Bitmap is null, but outWidth, outHeight, and outMimeType are set. This technology can obtain the size data and type data of Bitmap in advance.
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: Before decoding an image, check the Bitmap size in advance unless you are sure that your bitmap has a proper size and is within the memory limit.
Now that we have loaded the scaled image to the memory, we can determine whether to use this image or another sample. We should pay attention to the following factors: estimate the memory used to load the entire image. The memory usage you can receive and the memory that your program can use. The size of the ImageView you put in the image or the size of other UI components, for example, it is not worth placing an image of 1024x768 pixel in the 128x96 pixel Thumbnail Control (ImageVIew. We need to load a smaller sample of the image into the memory, and set inSampleSize to true in BitmapFactory. Options. For example, we want to decode a 2048*1536 image to a 1/4*512 image of Only 384 size, and load the image to the memory, which is only 0.75MB in size every year, the size of the source image is 12 Mb (using the ARGB_8888 channel ). Here is a method for calculating the sample image, based on the width and height of the target image width and height:
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;}
Note: A power of two value is calculated because the decoder uses a final value by rounding down to the nearest power of two, as per the inSampleSize documentation.
When using this method, we first set
inJustDecodeBounds
If the value is true, calculate the value of inSample, set inJustDecodeBounds to false, and finally decode the image.
。
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 can help us easily and effectively load a 100*100 thumbnail:
mImageView.setImageBitmap( decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
You can also use the same method to decode an image obtained from other paths.
BitmapFactory.decode*
As need. There are several other articles translated from android Developer below, which are very useful. Add attention to them (* ^__ ^ *).