我尝试了两种图片框架Picasso和Glide,用于从SD卡中加载图片并在RecyclerView的列表项的ImageView中显示。 RecyclerView列表包含大约50个以上的项目。
每当我向下滚动列表时,滚动不流畅,有时会开始出现卡顿,这会恶化用户体验。我只在片段内从磁盘(SD)加载图像。
对于Picasso,我使用以下代码:
对于Glide,我只是使用了以下代码示例:
每当我向下滚动列表时,滚动不流畅,有时会开始出现卡顿,这会恶化用户体验。我只在片段内从磁盘(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);
你有什么想法可以实现平滑滚动效果吗?