Displaying images and managing memory in android

前端 未结 2 1654
时光取名叫无心
时光取名叫无心 2021-01-07 09:14

I wrote a program that at any time displays 8 user selected images on the screen. Each image is taken from its original form and scaled down to a uniform size. In order to d

2条回答
  •  情歌与酒
    2021-01-07 10:03

    You can use BitmapFactory.Options to scale the image as you decode it rather than reading in a full image and then scaling it.

    BitmapFactory.Options options = new BitmapFactory.Options();
    // You can play with this setting depending on how large your images are
    // For example, to scale ~400x400 images to ~100x100, you can use 4.
    options.inSampleSize = 4;
    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    

    Edit - George is correct. You should use inSampleSize to create a smaller image of the general size you need and then have it resized to the exact size you want using your ImageView. I've corrected my answer above to reflect this.

    In any case, you should be much better off memory-wise if you are scaling the bitmaps during decode.

提交回复
热议问题