Resizing Images in Android for use in a listview
-
28-10-2019 - |
Question
I have a bunch of "large" 400x400 images that I am pulling from a source on the web and I need to display as 100x100 thumbnails in a ListView on Android. If I simply set the dimensions on the imageview element in xml it is very clear it is only scrunching the images down to 100x100 for 2 reasons:
1) The images don't look resampled 2) When given more than lets say 10 of these images in the list the scrolling becomes unbearably slow
I am not AS worried about #1 but the scrolling needs to stay smooth. So I am wondering, how should I go about resizing/resampling these images and caching them? I've done some googling but I just can't find a good resource for what would be a best practice in this situation.
Thanks in advance!
Solution
Take a look at BitmapFactory and the Options class. Options
has an inSampleSize
value that you can set to scale down an image when you read it in. You could try something like
Options op = new Options();
op.inSampleSize = 4;//You want to scale from 400 to 100
Bitmap thumbnail = BitmapFactory.decodeFile(fileName, op);
OTHER TIPS
Just set android:scaleType="fitCenter"
and your image will be scaled down to your desired size.