android - 使用 android Picasso 或 Glide 滚动时出现卡顿效果

标签 android performance picasso android-glide onscrolllistener

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

每当我向下滚动列表时,滚动 Action 都不流畅,有时会开始卡顿,这在我看来会恶化用户体验。我只从 fragment 中的光盘 (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 我只是使用了以下代码示例:

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);

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

最佳答案

尝试将其添加到您的 RecyclerView 中:

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

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

Source

关于android - 使用 android Picasso 或 Glide 滚动时出现卡顿效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37457628/

相关文章:

android - 线程和音频同时播放

java - 将 Blockly 生成的 XML 代码解析为 Java 对象

android - 启动画面大小调整不正确

java - RecyclerView 仅在滚动时加载图像

android - 如何在android中使用广播接收器获取应用程序的包名称?

javascript - 如何针对 IE 进行优化?

performance - IDE/编译器 PC 基准测试来比较我的 PC 性能?

perl - Perl 比 bash 快吗?

Android Picasso "fit().centerCrop()"不加载图像

android - picasso :从磁盘加载缓存图像太慢