Displaying images and managing memory in android

前端 未结 2 1627
时光取名叫无心
时光取名叫无心 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 09:57

    @matthew-willis I do not think you can use outWidth and outHeight to scale a bitmap. I believe they are output parameters only: they report the size of the bitmap created after the fact--setting them prior to decoding has no effect. You should use inSampleSize if you want to scale as you decode. George

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题