LoginSignup
4
2

More than 5 years have passed since last update.

PicassoでRecycleViewに画像を表示する場合はfit()しましょう

Posted at

RecycleViewに画像を表示させるとき(というか例外なく) fit() を指定するべきだと思います。
でないとスクロール時にカクツク!!!

最初に書いていたコード

RecycleViewのAdapterクラスにこんな感じで画像を表示させていました


override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
        val model = list[i]
        if (model.icon != null) {
            //ここで画像をダウンロードして表示させていた
            Picasso.with(context).load(`画像URL`).into(viewHolder.~~~)
        }
}

指定している箇所を抜き出すとこんな感じです。

Picasso.with(context).load(`画像URL`).into(viewHolder.~~~)

この実装だとスクロールした瞬間にかくかくします。

解決方法

ちゃんと fit() で画像にImageViewにfitさせるようにしましょう。
これを指定していないと内部ではでかい画像をImageViewにはめ込んでいることになっているので
スクロールしたときのパフォーマンスが悪くなります。

Picasso.with(context).load(`画像URL`).fit().centerCrop().into(viewHolder.~~~)
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2