I have to show an image into imageview in my layout for recycleview' s row.
RecycleView use ViewHolder pattern so my code is in custom ViewHolder.
To load the images I need to use an AsynkTask that return a File in onPostExecute(). In this onPostExecute() I setup imageView with Picasso.
I tried to obtain the same file without use AsynkTask. So I call directly Picaso.with()....
In this scenario, recycleview lag so much when scrolling, but i notified that Picasso cache all images.
The first scenario with AynkTask has more than one problem. Scrolling down and up for x times with 100 row make the queue of asynktask bigger.
Now, if I open a fragment of drawer, queue of Asynkask continues to load and this selected fragment make its work so slowly.
how can I fix it?
I would suggest you to checkout Image Management Library by Facebook that is Fresco that is pretty awesome and mature as compared to other Image Loading Library.
When image in a view goes out of screen it automatically recycles the bitmap, hence releasing the memory (Making scrolling smooth).
Fresco handles all the things caching of images with 3 Tier architecture ( BITMAP_MEMORY_CACHE, ENCODED_MEMORY_CACHE and DISK_CACHE).
It also reduces OOM (Out Of Memory) issues.
Related
I have a FragmentA(with Recyclerview) and a FragmentB(item). When I click in A to jump to B, the page lags a bit, maybe I updated the content in the main thread? But I don't seem to be doing that.
In FragmentB, I have some text and 3 Recyclerview, maybe the recyclerview cause the lag?
May be I need to load the data after the transition animation is complete? How to finish it?
It can be because of many things. Check if you are using a large image size that can use more power and time to render. Try to move your long operations into coroutines.
As your layouts has one image, 3 recycler view & other stuff there is many cases for layout lagging.
May the image you are loading is larger. (Try: load it with Glide or other image loading libraries)
2.May your 3 recycler listing has large number of items & you are loading them immediately after creating your fragment view. (Try: put them into the runnanble & load them after some delay of a time)
I have a recyclerview that I want to display images that I have saved in drawables in jpeg. For loading I was using an Async Task, but this doesn't preload or Cache them. I was told to use one of the tools like fresco or glide to do that, but which one can do the Job better?
The most important thing for me is that the app doesn't crash, but it would also be nice to be able to preload certain images already before the View is displayed and afterwards preload as many as memory allows.
You can use Fresco for this. An example for a recycler view that loads images from the network can be found here:
https://github.com/facebook/fresco/blob/master/samples/showcase/src/main/java/com/facebook/fresco/samples/showcase/drawee/DraweeRecyclerViewFragment.java
I've been trying to create a Recycler View full of Card Views. Currently, I'm using the Recycler View from this tutorial and loading it with 14 different images. The professional quality images range in size from 134K to 242K.(Down from 8MB - 18MB)
I know that the images may be too big but, I feel there must be some way to get better performance while scrolling. Can someone point me in the right direction?
Edit: The images will be stored on the device. There will probably never be more than 20 of them.
You can use Picasso library or Android Universal Image Loader
Picasso
Android-Universal-Image-Loader
You don't need any AsyncTask.Theese 2 library handling image operations in background so you can keep scrolling smoothly. I am using Picasso in my project right now. You can add error drawable , temporary placeholder default drawable , and its so simple automatically caching.
Just use one of them in onBindViewHolder to bind any image to imageView
Load the images in a separate thread (or AsyncTask).
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.
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.