gpt4 book ai didi

java - ListView 滞后于音乐专辑图片

转载 作者:行者123 更新时间:2023-11-29 20:27:32 25 4
gpt4 key购买 nike

我正在构建一个音乐播放器,它应该有一个带有标题艺术家姓名和专辑封面图像的 ListView 。这似乎变得非常滞后。我怎样才能提高性能?

我获取这些图片的函数是这样的:

public static Bitmap getAlbumart(Context context,Long album_id){
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
try{
final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
if (pfd != null){
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
pfd = null;
fd = null;
}
} catch(Error ee){bm = BitmapFactory.decodeResource(context.getResources(),R.drawable.cd_128x128); }
catch (Exception e) { bm = BitmapFactory.decodeResource(context.getResources(),R.drawable.cd_128x128);}
return bm;}

如果我每次在我的 getView 函数中都这样做,应用程序就会自行终止。所以我尝试先将它们保存在位图数组列表中,但这需要很长时间。我已经尝试过使用 Viewholder 但这并不影响性能。

我的 getview 看起来像这样

`@Override
public View getView(int position, View view, ViewGroup parent) {

View rview = view;
holder = null;


if (rview == null)
{
LayoutInflater inflater = context.getLayoutInflater();
rview= inflater.inflate(R.layout.row_song, null, true);
holder = new ViewHolder(rview);
rview.setTag(holder);
}
else
{
holder = (ViewHolder) rview.getTag();
}


holder.imgAlbumart.setImageBitmap(Music.getAlbumart(context, Long.valueOf(AL_songlist.get(position).getAlbumID())));

holder.txtTitle.setText(AL_songlist.get(position).getTitle());
holder.txtArtist.setText(AL_songlist.get(position).getArtist());


return rview;
}`

编辑:enter image description here

我试了一下也是一样

android.provider.MediaStore.Audio.AlbumColumns.ALBUM_ART

最佳答案

使用我的 AlbumArtLoader.java。我是从 android 开发者网站上获得的,但找不到该页面。我对其进行了修改,使其比提供的代码更流畅。

/* Loads images smoothly in ListView */


public class AlbumArtLoader {
private Context ctx;
private int artSize;
private final Bitmap mPlaceHolderBitmap;
private Drawable[] drawables = new Drawable[2];
public AlbumArtLoader(Context c) {
ctx = c;
artSize = c.getResources().getDimensionPixelSize(R.dimen.albumart_size);
mPlaceHolderBitmap = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.transparent);
mPlaceHolderBitmap = Bitmap.createScaledBitmap(mPlaceHolderBitmap, artSize, artSize, false);
drawables[0] = new BitmapDrawable(ctx.getResources(), mPlaceHolderBitmap);
}
class BitmapWorkerTask extends AsyncTask<String, Void, TransitionDrawable> {
private final WeakReference<ImageView> imageViewReference;
private String path;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected TransitionDrawable doInBackground(String... params) {
path = params[0];
// TransitionDrawable let you to make a crossfade animation between 2 drawables
// It increase the sensation of smoothness
TransitionDrawable td = null;

// The albumart_unknown bitmap is recreated for each album without album art to maintain even scrolling
if(path == null) {
Bitmap b = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.albumart_unknown), artSize, artSize, true);
drawables[1] = new BitmapDrawable(ctx.getResources(), b);
} else {
Bitmap b;
try {
b = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), artSize, artSize, true);
} catch(Exception e) {
b = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.albumart_unknown), artSize, artSize, true);
}
drawables[1] = new BitmapDrawable(ctx.getResources(), b);
}
td = new TransitionDrawable(drawables);
td.setCrossFadeEnabled(true);
return td;
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(TransitionDrawable td) {
if(isCancelled()) {
td = null;
}
if(imageViewReference != null && td != null) {
final ImageView imageView = imageViewReference.get();
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if(this == bitmapWorkerTask && imageView != null) {
imageView.setImageDrawable(td);
td.startTransition(200);
}
}
}
}

public void loadBitmap(String path, ImageView imageView) {
if(cancelPotentialWork(path, imageView)) {
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(ctx.getResources(), mPlaceHolderBitmap, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(path);
}
}

static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}

public static boolean cancelPotentialWork(String path, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if(bitmapWorkerTask != null) {
final String bitmapData = bitmapWorkerTask.path;
// If bitmapData is not yet set or it differs from the new data
if(bitmapData == null || bitmapData != path) {
// Cancel previous task
bitmapWorkerTask.cancel(true);
} else {
// The same work is already in progress
return false;
}
}
// No task associated with the ImageView, or an existing task was cancelled
return true;
}

// Helper method
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if(imageView != null) {
final Drawable drawable = imageView.getDrawable();
if(drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
}

要使用它,请在您的 Adapter 中创建一个 AlbumArtLoader 实例

AlbumArtLoader mArtLoader;

实例化使用

mArtLoader = new AlbumArtLoader(context);

在您的Adapter 构造函数中,它从您的Activity 接收Context

然后在getView()

mArtLoader.loadBitmap(pathToAlbumArt, vh.imgAlbumart);

额外:从您的光标获取专辑艺术路径

int artColumnIndex = albumCursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ART);

if(albumCursor!=null && albumCursor.moveToFirst()) {
do {
String artPath = albumCursor.getString(artColumnIndex);
} while (albumCursor.moveToNext());
}

关于java - ListView 滞后于音乐专辑图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32351763/

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