Resizing Images in Android for use in a listview - android

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!

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);

Just set android:scaleType="fitCenter" and your image will be scaled down to your desired size.

Related

Loading images into big size imageviews

I know that ,according to android docs, that if you have a big image. Then you scale it down to size of the image view you are loading it to (using the decodeResources and Options class).
Now my question is, lets say you want to load background picture and the source image is of similar size, do you just load the image? Or do you actually scale it down a bit and then using the FITXY to stretch it?
I am trying to avoid Out Of Memory exceptions here
Thank you

Draw a big image in center_crop type

I have a big image(about 2Mb 1024 * 540 ARGB8888) which is got via net. This image will be shown in a ImageView which is 800px width and 400px height in CENTER_CROP scale type. And there are 12 this kind of ImageView in a listview.
My question is:
Does android load the whole image into memory in CENTER_CROP mode cause it is very slow when I slide the listview. Maybe I should clip the image before setImageBitmap()? Which is the efficient way?
Read the information from the link bellow. It will help you better to understand how to work with bitmaps in android and even in ListView. You should cash images so you will not download them anytime the ListView is refreshing.And yes,android loads all the image and after that centers it and crops,what you should do is to load in memory only a thumbnail of the all image and display it and not all the image. And as i wrote, you should cash them.
Android Bitmap managing

How many pictures can i put in my app?

I have a gallery working in my app at the moment with about 8 pictures on it. Before i had a bug that use to make it crash because of overload Heap (something like that, not sure).
This was caused by my pictures being too big in file size, so i reduced them it worked. So my main question is How many picture can i actually put in to my app.
I was hoping to have about 5 screens (activities) with some scrolling pictures
I don't want to start changing my app if its just going to crash again
Also does anyone know how to add transitions when scrolling through pictures, for it too look fancy
Everything depends on the way you manage you pictures. To save the memory (and increase the number of images loaded) you should load a resized picture.
You can subsample picture while loading:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
BitmapFactory.decodeResource(res, id, opts);
This will load a picture scaled 1/2 of it's original size (keep in mind that subsampling works only for power of 2 values). You can load the picture smaller than the area it will be displayed in and let ImageView to upscale it.
If your loaded image is still too large, you can scale it more with Bitmap.createScaledBitmap().
You should be aware that pre-Honeycomb Android doesn't recycle bitmaps like post-Honeycomb Android does. You should call Bitmap.recycle() as soon as the bitmap is not used anymore to clear the memory.
For image scrolling you can use ViewPager.
Further reading: http://developer.android.com/training/displaying-bitmaps/index.html
There is no strict limit on the number of pictures you can include in your app. See the stock Gallery app as an example, I consider myself a relatively light picture taker, but I still have hundreds of images in my Gallery. If you follow the Bitmap best practices you shouldn't run into any memory issues, and you should be able to include as many images as you want.

ListView items. Handle images with huge height

I have listview items with simple View inside, and i need to display image with huge height in it. I have cache system which can split large image into smaller ones.
Question:
What is the best way to handle displaying large images in one listview item?
Sure i can add some views to item at runtime(10 view 1000px height for example), but i think that i will get out of memory.
My point is to make my app display image like 9gag app.
9gag app
9gag view hierarchy
Do not load large bitmaps to memory consider loading a smaller version into memory, set inSampleSize to true in your BitmapFactory.Options object. For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384.
Use these common guidelines for loading large bitmaps

ListView with photos from Gallery

I have a listview that has a photograph from the gallery for each entry but it was blowing up and running out of memory so I resized the image
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 8;
Bitmap yourSelectedImage = BitmapFactory.decodeFile(photo,opts);
imgView.setImageBitmap(yourSelectedImage);
The problem I have is that each image in the list has to be resized before it is displaying the view.
Is there a way to link to the thumbnail view instead of the full size image so that I don't have to resize them or is there a way to load each picture as it is sized instead of having to wait for them all.
This basically sounds like exactly the sort of thing you want from droid-fu's remote image handling, only simpler (the "remote loading" part is really just scaling an image loaded from the Gallery instead of downloading one from the Web). You should be able to use very similar code. Alternatively, this is pretty close to this SO question about lazy-loading images.

Categories

Resources