android-image: load large bitmap Efficiently

來源:互聯網
上載者: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 should match the size of the UI component that displays it.(Although many 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 could 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 into.

  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 the 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;        }    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.