Android ListView加载图片很卡的问题,希望大神有办法帮我解决一下。

Android 码拜 9年前 (2015-04-24) 1353次浏览 0个评论

这是一个加载音乐图片的问题
下面是一个 adapter 的 getView 方法,在该方法中最后面 setImageBitmap 就是这里设置图片会导致listView滑动很卡。

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder holder;
		if (view == null) {
			view = inflater.inflate(R.layout.list_view_music_item, null);
			holder = new ViewHolder();
			holder.tvMusicTitle = (TextView) view.findViewById(R.id.itemName);
			holder.tvMusicDes = (TextView) view.findViewById(R.id.itemDetail);
			holder.albumPic = (ImageView) view.findViewById(R.id.listView_albumPic);
			holder.frm_menu_layout = (RelativeLayout) view.findViewById(R.id.frm_menu);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.frm_menu_layout.setOnClickListener(new MenuOnClickListener(position));
		if (MyApplication.getCurrentMusic() != null && 
				musics.get(position).getId() == MyApplication.getSharedPreferences()) {
			view.setBackgroundResource(R.drawable.item_background);
		} else {
			view.setBackgroundColor(Color.WHITE);
		}
		holder.tvMusicTitle.setText(musics.get(position).getTitle());
		holder.tvMusicDes.setText(musics.get(position).getArtist() + " - "
				+ musics.get(position).getAlbum());
		// 更新专辑图片
		holder.albumPic.setTag(musics.get(position).getData());
		holder.albumPic.setImageBitmap(ImageManager.loadMusicImage(activity,musics.get(position)));
		return view;
	}

然后我把 ImageManager 这个类贴出来,加载图片和默认图片的

/**
 * 加载图片管理类
 * @author Administrator
 *
 */
public class ImageManager {
	private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");

	public static Bitmap loadMusicImage(Context context, Music music) {
		if(music == null){
			//加载默认图片
			return ImageUtil.readResourcesBitmap(context, R.drawable.default_album_identify);
		}
		return loadMusicImage(context, music, R.drawable.default_album_identify);
	}

	public static Bitmap loadMusicImage(Context context, Music music, int defaultImage) {
		Bitmap bm = getArtwork(context, music.getId(), music.getAlbumId());
		if (bm == null && defaultImage!=0) {
			bm = ImageUtil.readResourcesBitmap(context, defaultImage);
		}
		return bm;
	}

	public static Bitmap getArtwork(Context context, long song_id, long album_id) {
		if (album_id < 0) {
			if (song_id >= 0) {
				Bitmap bm = getArtworkFromFile(context, song_id, -1);
				if (bm != null) {
					return bm;
				}
			}
			return null;
		}
		ContentResolver res = context.getContentResolver();
		Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
		if (uri != null) {
			InputStream in = null;
			try {
				in = res.openInputStream(uri);
				BitmapFactory.Options opt = new BitmapFactory.Options();
				opt.inPreferredConfig = Bitmap.Config.RGB_565;
				opt.inPurgeable = true;
				opt.inInputShareable = true;
				return BitmapFactory.decodeStream(in, null, opt);
			} catch (FileNotFoundException ex) {
				Bitmap bm = getArtworkFromFile(context, song_id, album_id);
				if (bm != null) {
					if (bm.getConfig() == null) {
						bm = bm.copy(Bitmap.Config.RGB_565, false);
					}
				}
				return bm;
			} finally {
				try {
					if (in != null) {
						in.close();
					}
				} catch (IOException ex) {
				}
			}
		}
		return null;
	}

	private static Bitmap getArtworkFromFile(Context context, long songid,long albumid) {
		Bitmap bm = null;
		if (albumid < 0 && songid < 0) {
			throw new IllegalArgumentException("Must specify an album or a song id");
		}
		try {
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
			opt.inInputShareable = true;
			if (albumid < 0) {
				Uri uri = Uri.parse("content://media/external/audio/media/"+ songid + "/albumart");
				ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
				if (pfd != null) {
					FileDescriptor fd = pfd.getFileDescriptor();
					bm = BitmapFactory.decodeFileDescriptor(fd, null, opt);
				}
			} else {
				Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
				ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
				if (pfd != null) {
					FileDescriptor fd = pfd.getFileDescriptor();
					bm = BitmapFactory.decodeFileDescriptor(fd, null, opt);
				}
			}
		} catch (FileNotFoundException ex) {

		}
		return bm;
	}
}
Android ListView加载图片很卡的问题,希望大神有办法帮我解决一下。
20分
用异步加载或者第三方的吧,搜一下imageloader,很多
Android ListView加载图片很卡的问题,希望大神有办法帮我解决一下。
20分
不要在UI线程中加载图片,异步加载好后直接设置到imageview中
可以使用Android-Universal-Image-Loader
https://github.com/nostra13/Android-Universal-Image-Loader
Android ListView加载图片很卡的问题,希望大神有办法帮我解决一下。
引用 2 楼 yexianghu 的回复:

不要在UI线程中加载图片,异步加载好后直接设置到imageview中
可以使用Android-Universal-Image-Loader
https://github.com/nostra13/Android-Universal-Image-Loader

谢谢,问题已解决了!


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Android ListView加载图片很卡的问题,希望大神有办法帮我解决一下。
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!