【问题标题】:How To Load Layout Background Using Picasso and cache it如何使用毕加索加载布局背景并缓存它
【发布时间】:2018-02-14 18:20:33
【问题描述】:

我的 View (LinearView) 有一个可绘制的背景。如何在 onSaveInstanceState 中保存 Drawable 以提高显示速度?其实图片是从网上下载的,下载的时候设置为查看背景。如果 onResume() 中的 background!=null 我不应该再次从 Internet 加载它。

我尝试了 Picasso,但我需要将图像加载到 View,而不是 ImageView,所以我使用了这样的东西:

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() {
            }
        });

它会下载图像,但它似乎没有缓存它,每次加载大约需要 4 秒...

我也尝试使用 MyClass 实现 Picasso.Target ,但它只是不调用 onBitmapLoaded() 方法。哪里有问题?

提前感谢大家的回答!

【问题讨论】:

    标签: android performance android-activity


    【解决方案1】:

    .into之前添加.networkPolicy(NetworkPolicy.OFFLINE)

    像这样:

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

    Read more

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多