zoukankan      html  css  js  c++  java
  • androidimage: load large bitmap Efficiently

      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;
            }
        }
  • 相关阅读:
    python使用virtualenv创建和管理虚拟环境
    花费一周刷完两份面试pdf(含答案)轻松拿下了抖音、头条、京东、小米等大厂的offer,成功度过程序员的寒冬。
    基于JAVA-SSM框架的B/S微博系统的设计与实现
    如何破解压缩文件zip,rar
    最新精仿Chinaz中国站长网整站源码带全部数据带采集功能
    淘宝自动发货源码,网店自动值守发货系统 不限制域名 支持客户自助提货及自动评价
    得到影视源码分享(有演示),带一键采集,亲测能用,适合懒人做电影站!
    JAVA汽车4S店管理系统
    H5传奇源码,附带微信支付,商城系统,新增了元宝交易商城系统源码
    jdk8的安装及卸载
  • 原文地址:https://www.cnblogs.com/slider/p/2986042.html
Copyright © 2011-2022 走看看