Search code examples
javaandroidperformancepicasso

How to save images with Picasso so there is no loading time?


I have an app where I display a quote and there is a picture in the background. When I click on a "next"-button, the next quote will be shown and the new image will be loaded. The loading takes around 0.5 seconds and has the image before as placeholder. But when I switch back, there is no loading time.

This means, the image is saved somewhere, so it does not need to be loaded again. Unfortunately, this is just temporarily. When I get to see the next 5 pictures and go back to the first one, the first picture needs to be loaded again. So I tried loading all the pictures in the beginning (it is just 25 pictures), like this:

Picasso.get().load(backgrounds.get(1));
Picasso.get().load(backgrounds.get(2));
Picasso.get().load(backgrounds.get(3));
Picasso.get().load(backgrounds.get(4));
Picasso.get().load(backgrounds.get(5));
Picasso.get().load(backgrounds.get(6));
Picasso.get().load(backgrounds.get(7));

and when I click on my "next"-button, I use this:

Picasso.get().load(backgrounds.get(counterBackground)).fit().noPlaceholder().into(background);

But both things without the expected effect. The images need a loading time around 0.5 every time and "noPlaceholder" doesn't work like expected, the placeholder is still there.

So, does someone know how to cut the loading time? Like, how to load all images in the beginning?

Thanks for every answer!


Solution

  • You can create cache while using Picasso, this will lead to quicker loading times (as long the images are cached

    Picasso picasso =  new Picasso.Builder(this).downloader(new OkHttpDownloader(getCacheDir(), 250000000)).build();
    Picasso.setSingletonInstance(picasso);
    

    source

    You can use indicators to see from where the image is loaded by using

    setIndicatorsEnabled(true)
    

    source

    You can also use callbacks to have an indication when the image is completed loading

    you can see more details here

    Edit

    1. You can use Picasso.get().load(R.drawable.pic).into(imageview) I think it will work also for mipmap, usually mipmap are used for errors / place holders for your resources (if you are not using drawables)

    2. 2.

    You can create your own policies about how to cache/load your images

    Link