使用Android Picasso或Glide

时间:2016-05-26 10:04:04

标签: android performance picasso android-glide onscrolllistener

我尝试了两个图像框架PicassoGlide来加载来自SD的图像并将其显示在 ReceyclerView 列表项 ImageView中即可。 ReceyclerView 列表包含大约50个项目以及更多。

每当我向下滚动列表时,滚动运动都不流畅,有时会开始出现口吃,这会使我认为的UX恶化。我只在片段内加载光盘(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);

您对如何实现平滑滚动运动有什么想法吗?

2 个答案:

答案 0 :(得分:1)

尝试使用

android:adjustViewBounds="true" 
在布局中的imageView中

答案 1 :(得分:1)

尝试将此添加到您的RecyclerView:

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

除了我使用.Fit().Resize()和删除.CenterCrop()之外。现在它就像一个魅力。

Source