使用 android Picasso 或 Glide 滚动时的卡顿效果

Stuttering effects during scroll using android Picasso or Glide

我尝试了两个图像框架 Picasso and Glide 从 SD 加载图像并将它们显示在 ReceyclerView 的 列表项的 ImageView 中。 ReceyclerView 列表包含大约 50 项及更多项。

每当我向下滚动列表时,滚动运动都不流畅,有时会开始卡顿,这在我看来会恶化用户体验。我只从片段中的光盘 (SD) 加载图像。

对于 Picasso 我使用了以下代码:

Picasso.with(activity.getApplicationContext())
    .load(Constants.ABS_PATH_USER_IMAGE + user.getPicture() + ".jpg")
    .tag(PicassoOnScrollListener.TAG)
    .resize(100, 100)
    .centerCrop()
    .transform(new CircleTransform())
    .placeholder(R.drawable.default_user)
    .error(R.drawable.default_user)
    .into(holder.thumbNail);

Picasso ScrollListener 看起来像:

public class PicassoOnScrollListener extends RecyclerView.OnScrollListener {
public static final Object TAG = new Object();
private static final int SETTLING_DELAY = 500;

private static Picasso sPicasso = null;
private Runnable mSettlingResumeRunnable = null;

public PicassoOnScrollListener(Context context) {
    if(this.sPicasso == null) {
        this.sPicasso = Picasso.with(context.getApplicationContext());
    }
}

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
    if(scrollState == RecyclerView.SCROLL_STATE_IDLE) {
        recyclerView.removeCallbacks(mSettlingResumeRunnable);
        sPicasso.resumeTag(TAG);

    } else if(scrollState == RecyclerView.SCROLL_STATE_SETTLING) {
        mSettlingResumeRunnable = new Runnable() {
            @Override
            public void run() {
                sPicasso.resumeTag(TAG);
            }
        };

        recyclerView.postDelayed(mSettlingResumeRunnable, SETTLING_DELAY);

    } else {
        sPicasso.pauseTag(TAG);
    }
}}

要将 ScrollListener 添加到 ReceyclerView,我执行了以下操作:

myRecyclerView.addOnScrollListener(new PicassoOnScrollListener(getContext()));

对于Glide我简单地使用了follwong代码示例:

Glide
    .with(mActivity.getApplicationContext())
    .load(Constants.ABS_PATH_USER_IMAGE + author.getPicture() + ".jpg")
    .centerCrop()
    .placeholder(R.drawable.default_user)
    .crossFade()
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .transform(new GlideCircleTransform(mActivity.getApplicationContext()))
    .into(holder.thumbNailAuthor);

您对如何实现平滑的滚动有任何想法吗?

尝试使用

android:adjustViewBounds="true" 

在布局中的 imageView 中

尝试将此添加到您的 RecyclerView:

recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);

此外,我使用 .Fit() 而不是 .Resize() 并删除了 .CenterCrop()。现在它就像一个魅力。

Source