Load bitmap lazy, just before it is seen - android

I've a Listview with images. I am using an adapter to display the images because i get just a URL from a web service. At the moment i load all images Async, that works but its very inefficient. I get 100-200 images / URLs and mostly i do not need all.
I am looking for a solution to load a bitmap just before it is seen.
for example:
Bitmap 1 (i see it on the screen -> load bitmap)
Bitmap 2 (i see it on the screen -> load bitmap)
Bitmap 3 (i don't see it on the screen but its the next one -> load bitmap)
Bitmap 4 (i don't see it, nothing to do)
Bitmap 5 (i don't see it, nothing to do)
How can I do this?

You should implement the Holder pattern for your listview. See the documentation here

I use Universal Image Loader library, and lazy loading works perfect, take a look at it here:
Universal Image Loader at Github

Related

How to display images from URL efficently

I need to display a list of images in my application, and i get those images from an API call as an URL. Now I'm using Glide to show them, but i don't like the loading effect it makes (blank space while loading, and the the image). Is there any way to instant show those images, without any loading time and possibly without downloading them?
Since the images are stored on a remote server there is no way to bypass the downloading process, however Glide makes sure to only download the image from remote server when necessary, as the docs state
By default, Glide checks multiple layers of caches before starting a
new request for an image:
Active resources - Is this image displayed in another View right now?
Memory cache - Was this image recently loaded and still in memory?
Resource - Has this image been decoded, transformed, and written to
the disk cache before? Data - Was the data this image was obtained
from written to the disk cache before? The first two steps check to
see if the resource is in memory and if so, return the image
immediately. The second two steps check to see if the image is on disk
and return quickly, but asynchronously.
If all four steps fail to find the image, then Glide will go back to
the original source to retrieve the data (the original File, Uri, Url
etc).
To resolve this,
but i don't like the loading effect it makes (blank space while
loading, and the the image)
You can instead add a default placeholder on the ImageView until the real image is downloaded. This will display a default image while your actual image is downloading and then after download completion will replace it with the actual one.
Glide
.with(context)
.load(url)
.placeholder(R.drawable.ic_placeholder) //your default placeholder resource
.into(imageView)
you can display images from url by two ways:
Without any third party library:
URL url = new URL("URL of image here");
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bitmap);
Second way is by using Picasso

Load ListView of bitmap efficiently and responsively

I have looked at Glide and understand that Glide doesn't take Bitmap (or a Future of Bitmap) parameter in load function.
My current approach is to fall back to manual way following the blog here. Multithreading For Performance, just replacing the download function to create the bitmap in real time.
I wonder is there any image libraries could handle this task? Thanks.
UPDATE:
Or I can actually save the Bitmap in advanced then only pass the file name to Glide. it loads the image the normal way. In this situation, I need to house-keep the cache file.

Wrong bitmap gets attached to imageview inside viewpager

I am making an application where I need to display PDF pages inside ViewPager. We create Bitmap from each PDF page and then set it to ImageView. Creating bitmap is expensive process so I decided to cache bitmap once it gets created. This also improved the performance in terms of memory and loading bitmap. Since this is not a http request so I couldn't use any image caching library like Picasso. Instead I have used google's this sample and added the conversion logic of PDF page to Bitmap here.But the problem is when I swipe pages wrong bitmaps gets attached to wrong ImageViews.
For example I am swiping to page no. 5 and before the bitmap loads if I move back to 3 then the bitmap which suppose to attached to 5th page gets wrongly attached to 3rd page.
I tried a lot but couldn't fix it. Any help would be greatly appreciated.

difference between Drawee and ImagePipeline in Fresco Image Loading Library

I am using Fresco Image Loader in my application in which uri is loaded into SimpleDraweeView. I have a list of images, sometimes i load the same image into two different views. In this case does fresco loads the image from cache into SimpleDraweeView or does it downloads the image from the network whenever needed and also where exactly do i need to use Drawee and ImagePipeline. Please help me.
Fresco library is built specifically to address concerns like this. To answer your first question, no, the image won't be downloaded twice. It won't even be decoded twice. Fresco avoids doing any duplicate work. You should be completely fine in using the same uri in multiple views at the same time.
As for your other question, Drawee operates on an API level above ImagePipeline. Those two are not competing. ImagePipeline is to Drawee as a Wheel is to a Car. If you want to display the image you should be using Drawee and not ImagePipeline. If you need to get the actual image bytes or to do something else with the Bitmap other than displaying it, you should use the ImagePipeline.

Android image view makes app slow

In my app I have a list view, which contains an image view. The image is loaded from bas64 encoded string. First the string is decoded and then converted it to bitmap and then the bitmap is loaded to the image view.
All the decoding is done in an async task and concurrency is handled according to the below documentation.
Processing Bitmaps Off the UI Thread
The problem is the app is scrolling slow, and all other async tasks are not executing after that.
any possible solutions?
You probably insterting a huge Bitmap into a tiny ImageView like assume the image is FHD 1920x1280 and your ImageView is 192x128. You should load a smaller or same size Bitmap into ImageView. I guess this is a reason of scroll lags. Also it could be that your layout is too complex and should be optimized.
As for
async tasks are not executing after that
nobody can tell you anything without seeing your code.
If you are getting image URL from server you can store if you want to in local DB. Don't store base64 in database just store the image URL's.But for image loading you can simply use libraries that are available like Glide or Picasso for image loading. Your list view is lagging as you are doing heavy operation like decoding base64 and loading that bitmap. Just give it a try, it will work. You can load image using single line code like
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
you might need to put images into a folder "drawable-nodpi", since normaly android app will try to resize large image first, then load it, then show show the original image(by resize again), this process will cost much CPU which make your app respond slow. So just create a "drawable-nodpi" folder as the "drawable" folder, cut/paste all large images in the new folder

Categories

Resources