Optimal use of BitmapFactory.Options.inSampleSize for speed

前端 未结 4 1536
野的像风
野的像风 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 06:06

    You've asked good questions , but it all depends on your needs and how much memory you use. I recommend checking out this link for many tips regarding bitmaps: http://developer.android.com/training/displaying-bitmaps/index.html .

    In short , you should consider caching , downsampling , and using a good-enough bitmap format whenever you can.

    Here's my answers to your questions:

    1. Why not both? if you think there might be OOM , try to recycle old,unused bitmaps and then check again .

    2. you can calculate the (estimated) size of the bitmap :

      width*height*bytesPerPixel

      where bytesPerPixel is usually 4 or 2 (depending on the bitmap format) .

    3. Never used setImageURI , so I can't help you with that. I suggest downloading images in a background thread (using asyncTask is one way to do so) and showing them when it's ready.

    4. If there are only a few that you know that won't take a lot of the memory , i guess it's ok. I still think caching could be better.

提交回复
热议问题