在使用Android Picasso或Glide进行滚动时,出现口吃的影响

3
我尝试了两种图片框架PicassoGlide,用于从SD卡中加载图片并在RecyclerView的列表项的ImageView中显示。 RecyclerView列表包含大约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);
    }
}}

为了在RecyclerView中添加ScrollListener,我采取了以下步骤:

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

对于Glide,我只是使用了以下代码示例:
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);

你有什么想法可以实现平滑滚动效果吗?

2个回答

3
尝试将此添加到您的RecyclerView中:
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);

除此之外,我使用了 .Fit() 而不是 .Resize() 并移除了 .CenterCrop()。现在它完美地运行了。

源代码


2

尝试使用

android:adjustViewBounds="true" 

在布局中的ImageView。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接