• 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;
            }
        }
  • 相关阅读:
    go-web摘抄1-基础知识
    arduino3-机械臂
    Arduino-2 使用按键开关
    Arduino-1 点亮小灯
    树莓派的语音识别
    gitlab的环境搭建及使用
    python数据处理 2
    idea 无法创建class文件
    Intellij IDEA添加database无法显示表等问题
    Intell idea 添加 jd反编译插件
  • 原文地址:https://www.cnblogs.com/slider/p/2986042.html
Copyright © 2020-2023  润新知