适用于Android的Picasso库在为ImageView重复调用时表现不一致

时间:2013-12-10 22:36:11

标签: android imageview picasso

我在我的项目中使用Android的Picasso图像加载库。当我看到它将错误的图像加载到使用ViewHolder模式的ArrayAdapter中的ImageView时,我注意到了这个错误。

这是bug的关键所在,我现在可以在我创建的测试活动中一致地重复。我在onCreate中调用了这个方法:

private void refreshImageView() {
    ImageView imageView = (ImageView)this.findViewById(R.id.image);

    for (int i = 0; i < 10; i++) {
        new Picasso.Builder(this).build()
        .load(Uri.parse("http://192.168.1.1:4568/images/red.png"))
        .placeholder(R.drawable.silhouette)
        .noFade()
        .into(imageView);
    }

    new Picasso.Builder(this).build()
    .load(Uri.parse("http://192.168.1.1:4568/images/blue.png"))
    .placeholder(R.drawable.silhouette)
    .noFade()
    .into(imageView);
}

我还向ImageViewAction.java添加了一些调试输出。这是我添加的内容:

String str = bitmapToString(result);
Log.i("PicassoDebug", String.format("Complete and loading %s into %s", str.substring(str.length() - 20, str.length() - 1), target));

这是输出:

12-10 17:08:37.061: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.061: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ........ 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.121: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.121: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.171: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.171: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.221: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.221: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.281: D/dalvikvm(16411): GC_FOR_ALLOC freed 1766K, 22% free 16413K/20988K, paused 32ms, total 33ms
12-10 17:08:37.281: I/PicassoDebug(16411): Complete and loading ewAAAAASUVORK5CYII= into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.341: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.341: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ........ 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.391: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.391: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.441: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.441: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.501: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.501: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.551: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.551: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ........ 333,482-466,615 #7f08003b app:id/image}
12-10 17:08:37.601: I/PicassoDebug(16411): Complete and loading nbAAAAAElF
12-10 17:08:37.601: I/PicassoDebug(16411): TkSuQmCC into android.widget.ImageView{42067f38 V.ED.... ......ID 333,482-466,615 #7f08003b app:id/image}

以“nbAA”开头的行是red.png,以“ewAA”开头的行是blue.png。如您所见,红色的一些加载请求在蓝色后完成,因此显示为红色而不是蓝色。

毕加索有错误吗?或者这是“按设计”?如果是设计,在ArrayAdapters中使用Picasso的建议设计模式是什么,在更新之前经常会在同一个项目上重复调用getView()?

1 个答案:

答案 0 :(得分:1)

想出来:

当我调用新的Picasso.Builder(this).build()时,我正在使用一个新的targetToAction hashmap创建一个新对象,该对象不包含先前的请求。

解决此问题的正确方法如下:

private void refreshImageView() {
    ImageView imageView = (ImageView)this.findViewById(R.id.image);

    Picasso p = new Picasso.Builder(this).build();
    for (int i = 0; i < 10; i++) {
        p.load(Uri.parse("http://192.168.1.1:4568/images/red.png"))
        .placeholder(R.drawable.silhouette)
        .noFade()
        .into(imageView);
    }

    p.load(Uri.parse("http://192.168.1.1:4568/images/blue.png"))
    .placeholder(R.drawable.silhouette)
    .noFade()
    .into(imageView);
}

我的错误在于将毕加索视为全球单身人士。事实并非如此。