Crop and resize images in Android

I am reading an image from disk and displaying it inside a line in ListView. Image files are larger than what needs to be displayed inside the lines ImageView. Since I need to cache bitmapsin RAM for faster access, I would like them to be only the size of ImageView(85x85 dip)

I am reading in a file now

bitmap = BitmapFactory.decodeFile (file);

and ImageView is responsible for scaling and cropping

Android: scaleType = "centerCrop"

AFAIK is keeping the entire bitmap in memory (because I cached it with XD), and that is bad.

How can I remove this responsibility from ImageView and do a frame + scale when uploading a file? All bitmaps will be displayed with a 85x85 dip and should be "centerCrop"

+3
source share
1 answer

You can find out the sizes of your images before loading, cropping and scaling:


BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    Bitmap bmo = BitmapFactory.decodeFile(file.getAbsolutePath(), options);

Then load it in sample size:


...
options.inSampleSize = 1/2;
bmo = BitmapFactory.decodeFile(file.getAbsolutePath(), options);

...
 = Bitmap.createScaledBitmap(bmo, dW, dH, false);

Remember to recycle temporary bitmaps or you will get OOME.


bmo.recycle();
+5
source

Source: https://habr.com/ru/post/1783112/


All Articles