【问题标题】:How to access Drawable when using Picasso?使用毕加索时如何访问Drawable?
【发布时间】:2013-09-04 22:44:25
【问题描述】:

我正在使用 Picasso 框架来处理我的 Android 应用程序中的图像加载。 加载图像后,我需要访问 Drawable 以应用一些遮罩操作。问题是 Picasso 将 Drawable 转换为 PicassoDrawable,并且简单地转换回 Drawable 不起作用。

这是我的代码:

        Picasso.with(mContext).load(image.getPath()).into(mImageView, new Callback() {

            @Override
            public void onSuccess() {

                Util.applyMask(imageView);
            }

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

和 Util.applyMask(ImageView) 方法:

public static void applyMask(ImageView imageView) {

    // this is where a class cast exception happens since it's actually a PicassoDrawable and not a Drawable
    Bitmap mainImage = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

// ... 
}

Jake Wharton 在这个 github 问题中给出了一个可能的解决方案:https://github.com/square/picasso/issues/38

总而言之,解决方案是:“如果要直接访问位图,则需要使用目标回调。PicassoDrawable 用于允许淡入淡出和调试指示器。”

我不确定如何访问 Target 回调。有谁知道如何解决这个问题?

谢谢。

【问题讨论】:

    标签: android android-drawable picasso


    【解决方案1】:

    这是在 github (https://github.com/square/picasso/issues/38) 上回答的:

    private Target target = new Target() {
          @Override
          public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {       
          }
    
          @Override
          public void onBitmapFailed() {
          }
        }
    
    private void loadBitmap() {
       Picasso.with(this).load("url").into(target);
    }
    

    【讨论】:

    • 您应该考虑改为通过Transformation 应用掩码。这将避免在主线程上工作,并且毕加索会将最终位图缓存在内存中。如果您再次要求它,您将不必再做这项工作。
    • 好提示,我不知道毕加索还缓存了最终的转换位图。 (我会在 36 分钟内接受答案,只要 stackoverflow 允许我 :))
    • 不要忘记取消 onDestroy 中的 Target:github.com/square/picasso/issues/38#issuecomment-23793609
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 2014-10-10
    • 2018-03-07
    • 2018-03-10
    • 2015-10-16
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多