How To Load Layout Background Using Picasso and cache it

Ening Source

I have View (LinearView) with a drawable on it's background. How can I save Drawable in onSaveInstanceState to increase showing speed? Actually picture is downloaded from the Internet, and when it's downloaded it is set as View background. If background!=null in onResume() I shouldn't load it from the Internet again.

I tried Picasso, but I need to load image into View, not ImageView, so I used something like this:

final ImageView img = new ImageView(view.getContext());
Picasso.with(img.getContext())
        .load(mImagesUrls[index])
        .into(img, new com.squareup.picasso.Callback() {
            @Override
            public void onSuccess() {
               view.setBackground(img.getDrawable());
            }

            @Override
            public void onError() {
            }
        });

It downloads image, but it doesn't seem that it's caching it, every loading takes about 4 sec...

Also I tried to use MyClass implements Picasso.Target , but it just doesn't call onBitmapLoaded() method. Where can be a problem?

Thanks everyone for answers in advance!

androidperformanceandroid-activity

Answers

answered 2 weeks ago 0x7067 #1

Before .into add .networkPolicy(NetworkPolicy.OFFLINE)

Like this:

.load(mImagesUrls[index])
.networkPolicy(NetworkPolicy.OFFLINE)
        .into(img, new com.squareup.picasso.Callback() {

Read more

comments powered by Disqus