当前位置: 首页 > 知识库问答 >
问题:

如何提高recyclerview中的图像滚动性能(在recyclerview中滚动时应用程序滞后)?

西门鹏程
2023-03-14

我正在做一个音乐播放器。因此,在相册部分,我尝试加载毕加索库的相册图像。图像加载正确,但当我滚动时,就会出现应用滞后问题。我也尝试使用glide,但性能没有改善。我认为此问题是由于此链接的图像加载而出现的。“content://media/external/audio/albumart“。是否有其他方法?如果没有,那么如何提高滚动性能..”

public class Song_Adapter extends 
RecyclerView.Adapter<Song_Adapter.MyViewHolder> implements 
FastScrollRecyclerView.SectionedAdapter {


ArrayList<Song_base> song_info;
Context context;
LayoutInflater inflater;
LayoutInflater inflater1;
Home home;
AlertDialog.Builder dialog;
AlertDialog dg;
TextView remove,play,search,send,set_ringtone,detail;
DatabaseHelperAdapter helper;


public Song_Adapter(ArrayList<Song_base> song_info, Context context) {
    this.song_info = song_info;
    this.context = context;
    inflater = LayoutInflater.from(context);
    inflater1 = LayoutInflater.from(context);

}

@Override
public Song_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = inflater.inflate(R.layout.recycler_song_layout, parent, false);
    return new Song_Adapter.MyViewHolder(v);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) 
 {

    final Song_base song = song_info.get(position);

    holder.song_name.setText(song.getSong_name());
    holder.artist.setText(song.getArtist());

    holder.duration.setText(Constants.calculatetime(Integer.parseInt(
    song.getDuration())));


     getimageart(song.getAlbumId(), song.getContext(), holder.bannerp 


}


@Override
public int getItemCount() {
    return song_info.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView song_name, artist,duration;
    CardView cardView;
    Button play;
    ImageView bannerp;
    RelativeLayout relativeLayout;

    public MyViewHolder(View itemView) {
        super(itemView);
        song_name = itemView.findViewById(R.id.song_name);
        artist = itemView.findViewById(R.id.artist);
        cardView = itemView.findViewById(R.id.cardview);
        bannerp = itemView.findViewById(R.id.small_banner);
        relativeLayout = itemView.findViewById(R.id.cardrelative);
        duration = itemView.findViewById(R.id.duration);

    }


}

public static void getimageart(Long albumId,Context context,ImageView image) {

    Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
    Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);


    Bitmap bitmapimage = null;
    try {
        bitmapimage = MediaStore.Images.Media.getBitmap(
                context.getContentResolver(), albumArtUri);

    }
    catch (Exception exception) {
        exception.printStackTrace();

    }
    if(bitmapimage!=null)
    {
        Picasso.with(context)
                .load(Uri.parse(String.valueOf(albumArtUri)))
                .resize(200,200)
                .centerInside()
                .into(image);

    }


}


}

共有1个答案

柳梓
2023-03-14

使用回收器视图缓存,这将帮助您

recyclerView.setItemViewCacheSize(25);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

使用适合()

Picasso.with(context)
            .load(Uri.parse(String.valueOf(albumArtUri)))
            .resize(200,200)
            .fit()
            .centerCrop()
            .into(image);

fit()实际上调整了图像的大小以适应ImageView的边界。这不会加载完整的图像并使滚动平滑。

 类似资料:
  • 问题内容: 我已经基于“ 创建列表和卡片”指南创建了RecyclerView示例。我的适配器具有仅用于扩大布局的模式实现。 问题是滚动性能差。 这在RecycleView中只有8个项目。 在某些测试中,我验证了在Android L中不会发生此问题。但是在KitKat版本中,性能下降是显而易见的。 问题答案: 我最近遇到了同样的问题,因此这是我对最新的RecyclerView支持库所做的工作: 用

  • 问题内容: 我想知道,当我向下或向上滚动时,我的图像和布局的颜色会乱七八糟,我使用recyclerview创建了cardview。并设置图片(在点击时更改颜色,以了解其用户喜欢的项目),并将setbackgroundcolor(随机选择)设置为父版面,以使Cardview更具吸引力。但是当我滚动1.图像更改位置的图像时,2.布局背景自动更改颜色。 我在这里发布适配器的代码。 } 问题答案: 我已经

  • 如何禁用滚动?

  • 如何在recyclerview中滚动到具有偏移量的特定位置? 我用过 我也曾经 此外,我使用了skrolltopoplace 但似乎没有一个适合我。 如果有人知道这件事,你能帮我吗 使现代化 功能,其中我设置适配器的我的回收站

  • 我已经设置了一个回收器视图,每行都有文本视图和隐藏按钮。 如果用户单击该项,就会显示按钮。 如果用户单击其他项,则按钮将被隐藏。 再循环视图将位置发送到每个接口的活动,该活动更改其属性。 只要不滚动recyclerview,一切都能完美运行。 Layoutmanager.getchildat(职位) 现在,当项目超出视图时,用户滚动recyclerview,位置就会出错。每向下滚动一个用户单击的项