【问题标题】:How can I remove the lag of scroll when loading images from Internal Storage?从内部存储加载图像时如何消除滚动滞后?
【发布时间】:2019-12-21 06:06:40
【问题描述】:

我正在尝试从 GridView 中的手机内部存储加载图像。 GridView 在一个片段中。我有一个类存储所有图像的文件路径。我初始化班级的数据一次,这样我就不必一次又一次地进行。然后我使用该类将适配器设置为 GridView。

这是我的数据类的功能。

public static void initialize(Cursor cursor) {
    int count = cursor.getCount();
    allPaths = new String[count];

    for (int i = 0; i < count; i++) {
        cursor.moveToPosition(i);
        int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);

        allPaths[i] = cursor.getString(dataColumnIndex);
    }
    cursor.close();
}

这是我的 Grid 的适配器:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView mImageView;

    if (convertView == null) {
        mImageView = new ImageView(mContext);
        mImageView.setLayoutParams(new GridView.LayoutParams(250, 250));
        mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    } else {
        mImageView = (ImageView) convertView;
    }
    File f = new File(AllScreenshots.get(position));
    Picasso.get().load(f).into(mImageView);
    return mImageView;
}

图像的加载、网格视图和应用的滚动变得如此缓慢。 如何让加载、滚动和应用再次流畅快速?

【问题讨论】:

标签: android image performance picasso lag


【解决方案1】:

我使用了 RecyclerView 和 Glide 库,而不是 GridView 和 Picasso。在我的 RecyclerView 适配器的 onBindViewHolder 方法中,我使用了 glide 的方法

Glide.with(context).load(AllScreenshots.get(position)).thumbnail(0.5f).diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.img);

它为我解决了这个问题。

【讨论】:

  • 非常感谢!我什么都试过了!!包括毕加索图书馆,但没有任何效果,我改变了我的代码几个小时,你用一行解决了它,谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-07-10
  • 2019-03-31
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多