88

ImageView可変サイズ(幅と高さ)を含むアダプターを備えたlistViewがあります。Picasso を使用して、画像のサイズを変更して、レイアウトの最大幅と、画像の縦横比で指定された可変高さにサイズを変更する必要があります。

この質問を確認しました: Picasso で画像を全幅にリサイズし、高さを固定

動作しますfit()が、画像の縦横比を維持するものは何も見つかりませんでした。

アダプターのレイアウトで高さを修正した場合、このコードは部分的に機能します。

Picasso.with(this.context).load(message_pic_url)
.placeholder(R.drawable.profile_wall_picture)
.fit().centerInside()
.into(holder.message_picture);

ただし、listView の画像の間に空白が生成されます。これは、画像がその高さを持っていない可能性があるためです。

前もって感謝します。

4

12 に答える 12

94

Picasso 2.4.0 以降、この操作は直接サポートされるようになりました。.resize()ディメンションの 1 つを としてリクエストを追加するだけ0です。たとえば、幅を可変にするには、呼び出しは次のようになります。

Picasso.with(this.context)
       .load(message_pic_url)
       .placeholder(R.drawable.profile_wall_picture)
       .resize(0, holder.message_picture.getHeight()),
       .into(holder.message_picture);

この呼び出しは を使用しているため、がすでに測定されている.getHeight()と想定していることに注意してください。message_pictureで新しいビューを膨張させた場合など、そうでない場合は、ビューにListAdapterを追加して、測定後までこの呼び出しを遅らせることができOnGlobalLayoutListenerます。

holder.message_picture.getViewTreeObserver()
      .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            // Wait until layout to call Picasso
            @Override
            public void onGlobalLayout() {
                // Ensure we call this only once
                imageView.getViewTreeObserver()
                         .removeOnGlobalLayoutListener(this);


                Picasso.with(this.context)
                       .load(message_pic_url)
                       .placeholder(R.drawable.profile_wall_picture)
                       .resize(0, holder.message_picture.getHeight())
                       .into(holder.message_picture);
            }
        });
于 2014-11-06T14:35:37.893 に答える
86

同じ問題に遭遇し、解決策を突き止めるのにしばらく時間がかかりましたが、最終的に自分に合ったものに出会いました.

まず、ピカソの呼び出しを次のように変更しました

Picasso.with(this.context).load(message_pic_url)
.placeholder(R.drawable.profile_wall_picture)
.into(holder.message_picture);

と を削除しfitますcenterInside。次に、XML の ImageView に次の行を追加する必要があります。

android:scaleType="fitStart"
android:adjustViewBounds="true"

うまくいけば、それはあなたにとってもうまくいくでしょう。

于 2014-02-24T08:48:01.340 に答える
62

最後に、ピカソの変換を行って解決しました。スニペットは次のとおりです。

    Transformation transformation = new Transformation() {

        @Override
        public Bitmap transform(Bitmap source) {
            int targetWidth = holder.message_picture.getWidth();

            double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
            int targetHeight = (int) (targetWidth * aspectRatio);
            Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
            if (result != source) {
                // Same bitmap is returned if sizes are the same
                source.recycle();
            }
            return result;
        }

        @Override
        public String key() {
            return "transformation" + " desiredWidth";
        }
    };

    mMessage_pic_url = message_pic_url;

    Picasso.with(this.context)
        .load(message_pic_url)
        .error(android.R.drawable.stat_notify_error)
        .transform(transformation)
        .into(holder.message_picture, new Callback() {
            @Override
            public void onSuccess() {
                holder.progressBar_picture.setVisibility(View.GONE);
            }

            @Override
            public void onError() {
                Log.e(LOGTAG, "error");
                holder.progressBar_picture.setVisibility(View.GONE);
            }
    });

この行は、希望の幅でカスタマイズするためのものです:

int targetWidth = holder.message_picture.getWidth();

さらに、この省略されたインクルードには、非表示およびエラー ドローアブル ビルトイン Picasso をロードするためのコールバックが含まれます。

onError Callbackエラーをデバッグするためにさらに情報が必要な場合は、情報が「null」であるため、カスタム リスナー (Picasso ビルダー) を実装する必要があります。UI の動作にエラーがあることだけがわかります。

これが誰かが何時間も節約するのに役立つことを願っています.

于 2014-02-25T09:30:43.187 に答える
11

受け入れられた回答はすべてに役立つ可能性がありますが、複数ViewHolderに対して複数をバインドしている場合は、変換Views用のクラスを作成し、からImageViewを渡すことでコードを減らすことができます。ViewHolder

/**
 * Created by Pratik Butani
 */
public class ImageTransformation {

    public static Transformation getTransformation(final ImageView imageView) {
        return new Transformation() {

            @Override
            public Bitmap transform(Bitmap source) {
                int targetWidth = imageView.getWidth();

                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                int targetHeight = (int) (targetWidth * aspectRatio);
                Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                if (result != source) {
                    // Same bitmap is returned if sizes are the same
                    source.recycle();
                }
                return result;
            }

            @Override
            public String key() {
                return "transformation" + " desiredWidth";
            }
        };
    }
}

からの呼び出しViewHolder:

Picasso.with(context).load(baseUrlForImage)
                     .transform(ImageTransformation.getTransformation(holder.ImageView1))
                     .error(R.drawable.ic_place_holder_circle)
                     .placeholder(R.drawable.ic_place_holder_circle)
                     .into(holder.mMainPhotoImageView1);

それがあなたを助けることを願っています。

于 2015-12-14T07:17:20.277 に答える
3
    Picasso.with(this).load(url).resize(1800, 1800).centerInside().into(secondImageView)

    <ImageView
        android:id="@+id/SecondImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:adjustViewBounds="true"
        android:layout_margin="10dp"
        android:visibility="gone"/>

これは、すべてのデバイスで画像の高さを変えるのに役立ちます

于 2016-05-31T12:59:12.060 に答える
1
imageView.post(new Runnable() {
      @Override public void run() {
        Picasso.with(context)
            .resize(0, imageView.getHeight())
            .onlyScaleDown()
            .into(imageView, new ImageCallback(callback, null));
      }
    });
于 2017-04-24T20:23:41.620 に答える
0

レイアウト完了リスナーの追加を処理し、レイアウト プロセスが完了したときに (imageView) を呼び出す単純なヘルパーを作成しました。

public class PicassoDelegate {

private RequestCreator mRequestCreator;

public PicassoDelegate(ImageView target, RequestCreator requestCreator) {
    if (target.getWidth() > 0 && target.getHeight() > 0) {
        complete(target, requestCreator);
    } else {
        mRequestCreator = requestCreator;
        target.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                v.removeOnLayoutChangeListener(this);
                complete((ImageView) v, mRequestCreator);
            }
        });

    }

}

private void complete(ImageView target, RequestCreator requestCreator) {
    if (target.getWidth() > 0 && target.getHeight() > 0) {
        requestCreator.resize(target.getWidth(), target.getHeight());
    }

    requestCreator.into(target);
}

}

したがって、たとえばフラグメントの onViewCreated() で、このように簡単に使用できます

new PicassoDelegate(customerPhoto, Picasso.with(getActivity()).load(user.getPhotoUrl()).centerCrop());
于 2015-09-02T18:35:34.520 に答える