1

プロジェクトで Android 用の Picasso 画像読み込みライブラリを使用しています。ViewHolder パターンを利用する ArrayAdapter の ImageView に間違った画像をロードしているのを見て、このバグに気付きました。

これがバグの核心であり、作成したテスト アクティビティで一貫して再現できるようになりました。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 です。ご覧のとおり、赤のロード リクエストの一部が青の後に完了したため、青の代わりに赤が表示されました。

ピカソにバグはありますか?それとも「設計による」ものですか?設計によるものである場合、更新前に同じ項目に対して頻繁に getView() を呼び出す ArrayAdapters で Picasso を使用するための提案された設計パターンは何ですか?

4

1 に答える 1

1

理解した:

new Picasso.Builder(this).build() を呼び出したとき、以前の要求を含まない新しい targetToAction ハッシュマップを使用して新しいオブジェクトを作成していました。

この問題を解決するための正しいアプローチは次のとおりです。

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

私の間違いは、ピカソをグローバルなシングルトンと考えていたことです。そうではない。

于 2013-12-10T22:54:12.577 に答える