gpt4 book ai didi

java - 在 RecyclerView 中滚动时加载位图无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 12:41:04 25 4
gpt4 key购买 nike

我正在我的音乐应用程序中加载专辑艺术品。因为我无法将它们加载到主线程上,所以我使用线程,并且它们变得困惑!

有时图像未以正确的尺寸加载或显示为棕色方 block 。如果我快速滚动,就会出现这些问题。如果我滚动得慢,它就能工作!

我的 MusicStore 类的重要方法:

public Bitmap getAlbumArtwork(long AlbumID, int Height, int Width) {
ParcelFileDescriptor pfd;

Bitmap bCover = null;
BitmapFactory.Options bOptions = new BitmapFactory.Options();
bOptions.inJustDecodeBounds = true;

try {
Uri ArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(ArtworkUri, AlbumID);
pfd = mContext.getContentResolver().openFileDescriptor(uri, "r");

if (pfd != null) {
BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor(), null, bOptions);

bOptions.inSampleSize = calculateInSampleSize(bOptions, Width, Height);

bOptions.inJustDecodeBounds = false;

bCover = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor(), null, bOptions);

pfd.close();
}
}
catch (IOException ioe) {
BitmapFactory.decodeResource(mContext.getResources(), R.drawable.standardartwork, bOptions);

bOptions.inSampleSize = calculateInSampleSize(bOptions, Width, Height);

bOptions.inJustDecodeBounds = false;

bCover = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.standardartwork, bOptions);
}

return bCover;
}

public void setAlbumArtwork(final long AlbumID, final ImageView ArtworkView) {
Thread thArtwork = new Thread(new Runnable() {
@Override
public void run() {
final Bitmap bArtwork = getAlbumArtwork(AlbumID, ArtworkView.getHeight() / 2, ArtworkView.getWidth() / 2);

handler.postDelayed(new Runnable() {
@Override
public void run() {
ArtworkView.setImageBitmap(bArtwork);

threadList.remove(Thread.currentThread());
}
}, 50);
}
});

threadList.add(thArtwork);

thArtwork.start();
}

private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;

int size = 1;

if (height > reqHeight && width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;

while ((halfHeight / size) > reqHeight && (halfWidth / size) > reqWidth) {
size *= 2;
}
}

return size;
}

还有我的 RecyclerViewAdapter:

public class SongRecyclerViewAdapter extends RecyclerView.Adapter<SongRecyclerViewAdapter.Holder> {
private Context mContext;
private Song[] sSongs;

private MusicStore musicStore;

public SongRecyclerViewAdapter(Context context, Song[] songs) {
mContext = context;
sSongs = songs;

musicStore = new MusicStore(mContext);
}

@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_songview, parent, false);

Holder holder = new Holder(view);

return holder;
}

@Override
public void onBindViewHolder(Holder holder, int position) {
musicStore.setAlbumArtwork(sSongs[position].getAlbumID(), holder.imvSong);

holder.txvSongTitle.setText(sSongs[position].getTitle());
holder.txvSongInfo.setText(sSongs[position].getArtists());
}

@Override
public void onViewDetachedFromWindow(Holder holder) {

}

@Override
public int getItemCount() {
return sSongs != null ? sSongs.length : 0;
}

public class Holder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
ImageView imvSong;
TextView txvSongTitle;
TextView txvSongInfo;

public Holder(View layout) {
super(layout);

linearLayout = (LinearLayout) layout;

imvSong = (ImageView) layout.findViewById(R.id.imvSong);
txvSongTitle = (TextView) layout.findViewById(R.id.adap_txvSongtitle);
txvSongInfo = (TextView) layout.findViewById(R.id.adap_txvSongInfo);
}
}
}

我绝对愿意接受任何其他正确加载位图的想法!

如果你愿意,你可以尝试一下: https://play.google.com/store/apps/details?id=at.guger.musixs

谢谢!

最佳答案

http://square.github.io/picasso/

设置和文档非常简单。您也可以传入 Uri,Picasso 会解决它。

示例

Picasso.with(this).load(uri).into(imageView);

如果您还可以指定宽度和高度、占位符等内容。

.resize(width, height).placeholder(placeholderImg)

关于java - 在 RecyclerView 中滚动时加载位图无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36826881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com