Optimal use of BitmapFactory.Options.inSampleSize for speed

前端 未结 4 1546
野的像风
野的像风 2021-02-05 05:15

Thanks to Schermvlieger for asking this question on anddev.org,

I\'m just copying his question to SO as nobody replied on th

4条回答
  •  庸人自扰
    2021-02-05 05:59

    Here you can call the user defined method shrinkmehtod that actually send the string file path and the height and width to be reduce image to method.

     Bitmap bit=shrinkmethod(arrpath1[position], 100, 100);
    
    
                //iv.setImageURI(Uri.parse(arrpath1[position]));
                iv.setImageBitmap(bit);
    

    This is user defined method to reduce the size of image programmatically.

    Bitmap shrinkmethod(String file,int width,int height){
            BitmapFactory.Options bitopt=new BitmapFactory.Options();
            bitopt.inJustDecodeBounds=true;
            Bitmap bit=BitmapFactory.decodeFile(file, bitopt);
    
            int h=(int) Math.ceil(bitopt.outHeight/(float)height);
            int w=(int) Math.ceil(bitopt.outWidth/(float)width);
    
            if(h>1 || w>1){
                if(h>w){
                    bitopt.inSampleSize=h;
    
                }else{
                    bitopt.inSampleSize=w;
                }
            }
            bitopt.inJustDecodeBounds=false;
            bit=BitmapFactory.decodeFile(file, bitopt);
    
    
    
            return bit;
    
        }
    

    I hope this will help you to reduce size.

提交回复
热议问题