【问题标题】:How to clear images loaded by Picasso on Android?如何在 Android 上清除 Picasso 加载的图像?
【发布时间】:2017-04-21 20:34:00
【问题描述】:

我有一个使用毕加索加载背景图像的 BackgroundImageActivity。但是当我回到家时(在 BackgroundImageActivity 中调用 onStop())并进入该活动的另一个实例,该实例应该加载另一个背景图像,但在前 2 秒,它仍然显示来自先前 BackgroundImageActivity 的图像。这是某种缓存吗?

如何清除此图像,以便每当我进入 BackgroundImageActivity 的新实例时,我都看不到前一个图像?

public class BackgroundImageActivity extends Activity {

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Picasso.invalidate(imageUrl, getApplicationContext());
    }

    private void initBackground() {
        ...
    }

    private void setBg(final String imageUrl, final int bg) {

        this.imageUrl = imageUrl;

        final RequestCreator picassoRequest = Picasso.load(imageUrl, bg).memoryPolicy(MemoryPolicy.NO_CACHE);

        targetReference = new Target() {
            @Override
            public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
                ...
            }

            @Override
            public void onBitmapFailed(final Drawable errorDrawable) {
                ...
            }

            @Override
            public void onPrepareLoad(final Drawable placeHolderDrawable) {
                ...
            }
        };

        picassoRequest.placeholder(bg).error(bg).into(targetReference);
    }


}

谢谢!!!

【问题讨论】:

    标签: android android-layout android-activity picasso


    【解决方案1】:

    在加载图像时使用这段代码。

     Picasso.with(getContext()).load(data.get(pos).getFeed_thumb_image()).memoryPolicy(MemoryPolicy.NO_CACHE).into(image);
    

    picasso 直接将图片设置为 ImageView 而不在后台缓存。这也使应用程序轻量级。

    【讨论】:

      猜你喜欢
      • 2023-01-29
      • 2016-07-06
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 2016-06-25
      • 1970-01-01
      相关资源
      最近更新 更多