Android-image: Load large bitmap efficiently

Source: Internet
Author: User

An image with 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.

Given that you are working with limited memory, ideally you only want to load a lower resolution version in memory.Note that the lower resolution version shocould match the size of the UI component that displays it. (Although knows people know how to load large image, but that it do not meet their feeling. yeah, you must put subsample matching the size of the UI component that displays it .)

  According to offical site, you cocould load large bitmap as follows:

First, you should check the dimensions of a bitmap before really decoding it.

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;

Second, load a scale down version into memory. Here are some factors to consider:

1. Estimated memory usage of loading the full image in memory.

2. Amount of memory you are willing to commit to loading this image any other memory requirements of your application.

3. Dimensions of the target imageview or UI component that the image Si to be loaded.

4. screen size and density of the current device.

To tell the decoder to subsample the image, loading a smaller version into memory, set insamplesize to true in your bitmapfactory. options object. here's a method to calculate a sample size value based on a target 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) {        // 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;}

To use method above-mentioned, I create a class as follows:

/**     * requests the decoder to subsample the original image, returning a smaller image to save memory.     * it could receive too large image. and set default value of width,height with 100,100     * @param filePath     * @return     */    public static Bitmap getImageLocal(String filePath){        return getImageLocal(filePath,BitmapUtil.REQUEST_WIDTH,BitmapUtil.REQUEST_HEIGHT);    }        public static Bitmap getImageLocal(String filePath, int reqWidth, int reqHeight){        if(reqWidth==-1||reqHeight==-1){ // no subsample and no            return BitmapFactory.decodeFile(filePath);        }else {            // First decode with inJustDecodeBounds=true to check dimensions            final BitmapFactory.Options options = new BitmapFactory.Options();            options.inJustDecodeBounds = true;            BitmapFactory.decodeFile(filePath, options);                        // Calculate inSampleSize            options.inSampleSize = BitmapUtil.calculateInSampleSize(options, reqWidth, reqHeight);            Log.d(TAG, "options inSampleSize "+options.inSampleSize);            // Decode bitmap with inSampleSize set            options.inJustDecodeBounds = false;            return BitmapFactory.decodeFile(filePath, options);        }    }/**     * Purpose: calculate option for a bitmap, now define default value of width,height with 100,100     * Created Time: Mar 27, 2013 2:57:36 PM      * Update By: Slider Xiao, Mar 27, 2013 2:57:36 PM     */    static class BitmapUtil {        public static final int REQUEST_WIDTH = 100;        public static final int REQUEST_HEIGHT = 100;                public static int calculateInSampleSize(Options options){            return calculateInSampleSize(options, REQUEST_WIDTH, REQUEST_HEIGHT);        }        public static int calculateInSampleSize(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;        }    }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.