Most efficient way to display a grid of images - android

What is the most efficient way to display a grid of random images on android?
I have a list of random album art images, I need to generate a grid out of them and use it as a background in my activity, the images are downloaded asynchronously, scaled down and cached, displaying them in a grid seems to consume a lot of RAM,(yes I'm recycling the bitmaps, and using LRU cache)
Would drawing them to a canvas be a better solution? Are there other efficient ways to do that?
Is the GridView safe, are there any guarantees that it won't run out of memory?
P.S. drawing them to a canvas would require me to redraw when the orientation or activity's size changes.

I donĀ“t think there is a more efficient way, at least I can not think of one right now. What you could do, that depends if grid view is necessary, is using the Image carousel from Romain Guy which is made in Renderscript. I do not have the URL at the moment but it is a google-code project.
Another page where sometime are good stuff for those things is http://www.theultimateandroidlibrary.com/all
The LRUCache is really nice and fast and should do the trick. I also would be interested in a more efficient way.....

Related

Best way to manage bitmap storage in a long listview having big images?

I have a long Listview(~300) having big images(~720 * 300) in each row. Now if I scroll to the end of the Listview, my hashmap(which stores bitmaps) holds 300 images. Due to which manytimes I get out of memory exception. What is the best way to solve this problem?
Thanks for helping. Any code would be great!
You probably need a library like Picasso. Picasso helps you download the pictures, resize them if needed, and cache them. It also handles ListView specific problems, like freeing memory to ensure smooth scrolling.
In order to fix that you need to make the heap bigger in the Android Manifest,
also lazy loading if your loading them online and you have to reuse cells

Which approach to use to download many images asynchronously

I've a ListView in which each row item contains many ImageViews apart from some content.
That is, there will be many list items, each containing many images.
In order to not to hold user unnecessarily, I'm showing the content first & then downloading the images asynchronously.
Currently, for each image I'm starting a new AsycTask.
Although, it working alright, but this not a good approach.
In place of this, what should I use?
use this library, best in android for image loading..
https://github.com/nostra13/Android-Universal-Image-Loader
Your scenario seems to fit exactly to use LazyLoading of images. This basically loads images in the background and shows them as soon as they're loaded.
You might want to see this:
Lazy load of images in ListView
An alternative would be using a ThreadPoolExecutor with the images to load, but the above works pretty well and is recommended.
Try using the library
http://square.github.io/picasso/
Many common pitfalls of image loading on Android are handled automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.

ListView with many pictures thumbnails

I have a ListView with many lines (about 200).
In each item of the ListView there is a thumbnail picture.
The pictures are downloaded for form the internet as the ListView scrolls.
The problem is that if I scroll rapidly the ListView it gets sluggish and quickly I get an Out of Memory error.
Please could you suggest an approach on how to avoid this?
Use View Holder pattern on the custom adapter you're using with lazy loading.
Lazy Loading using Universal Image Loader.
OR
you could give Picasso / Volley Libraries a shot. They can handle caching efficiently for you.
The basic flow is to use a caching system:
Download Image to your cache directory -> read it back into memory cache -> scale it down (if the image is larger than the the image view that displays it) -> display it in your image view.
The official android training has a great tutorial along with full source code on how to create something like this.
They address the common issue of scrolling lag by pausing the background task(s) while user is scrolling.
Out Of memory is addressed through LruCache.
Alternatively you can use a third party library like the Universal Image Loader for addressing this issue.
You must handle your view recycling. It's a very important performance issue and you should always handle it.
See this post, it will guide you in this process, handling the view recycling and using the View Holder pattern. Also see this.
There are many reasons for recycling views:
Object creation is relatively expensive. Every additional object that is created needs to be dealt with by the garbage collection system, and at least temporarily increases your memory footprint;
This is more important for more complex views, but inflating and laying out the view objects can be expensive. Most often, you are only making minor changes to the view in getView that won't affect the layout (e.g, setting text) so you might be able to avoid the layout overhead;
Remember that android is designed to be run in a resource constrained environment;
Finally, its already done for you, and it certainly doesn't hurt anything, so why not use it;
Hoe it helps you.

android: most efficient way to repeat an image

I am doing a project where I want 100 of the same image randomly scattered throughout the screen. In the future, I want an image to disappear when the image is tapped by the user.
I am mainly focusing right now on the most efficient way to display a repeated image. It seems like I could set it up so that the fact that the images are the same makes it more efficient; I'm just not sure how. I do not want to proceed further until I know I have a sound base. I'm using .png files.
I've looked around without a definite answer.
Also, would if be easier to draw my object with two circles (which is what my image is), rather than using a bitmap?
Any clues???
Assuming you're talking about drawing the bitmap on a Canvas object, the method should be pretty straightforward. You load the image into a Bitmap object and keep it as a member of the owning class, and draw it 100 times using canvas.drawBitmap(...) functions.
The other way of doing it is having 100 ImageViews with the same image, but I won't even write the details because this would be truly inefficient!
You can use a listView: http://developer.android.com/guide/topics/ui/layout/listview.html
It shows a list of items, using an adapter to inflate elements. It is pretty simple to use.

Android. Loading an image inside an ImageView

What is the best way to load an image inside an ImageView?
I'm currently using .setImageResource() to do this, but i've heard that '.setBitmap()' is better, as you can make it not load the entire image in the memory before it resizes it. This post: Strange out of memory issue while loading an image to a Bitmap object
Which is the best, out of a device memory point of view? Which occupies less memory, which "stresses" the device the least?
Generally speaking, try not to use setImageResource() for larger images as this is executed on the UI thread (which may cause UI lag).
As for loading Bitmaps, I highly recommend this guide from Google. It covers asynchronous loading, using caches, general loading and much more. The tutorial came out within the last few months so uses the current best practices.
Placing your images in the imageview after re-sizing it is the best as it would avoid memory problems. setBitmap() is better.
If you have just one or two images, then .setImageResource would suffice. But if u have more number of images of varying sizes the setBitmap() is better.
Which is the best, out of a device memory point of view? - same one..
Which occupies less memory, which "stresses" the device the least? - same one!

Categories

Resources