patternjavaMinor
Efficient Album Cover loading for ListView
Viewed 0 times
albumefficientforloadingcoverlistview
Problem
I have a list view which display in each view a Album's name, the associated Artist and the album art.
Here is the code of my
Here is my custom
```
public class AlbumsAdapter extends CursorAdapter {
private final LayoutInflater mInflater;
public AlbumsAdapter(Context context, Cursor c) {
super(context, c);
mInflater=LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHold
Here is the code of my
ListFragment which display this list:public class AlbumsFragment extends ListFragment implements LoaderManager.LoaderCallbacks {
AlbumsAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.albums_fragment_layout, container, false);
return myFragmentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new AlbumsAdapter(getActivity(), null);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
static final String[] ALBUM_SUMMARY_PROJECTION = { MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ARTIST, MediaStore.Audio.Albums.ALBUM_ART,};
public Loader onCreateLoader(int id, Bundle args) {
String select = null;
return new CursorLoader(getActivity(), MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,ALBUM_SUMMARY_PROJECTION, select, null, null);
}
public void onLoadFinished(Loader loader, Cursor data) {
mAdapter.swapCursor(data);
}
public void onLoaderReset(Loader loader) {
mAdapter.swapCursor(null);
}
}Here is my custom
CursorAdapter's code:```
public class AlbumsAdapter extends CursorAdapter {
private final LayoutInflater mInflater;
public AlbumsAdapter(Context context, Cursor c) {
super(context, c);
mInflater=LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHold
Solution
Your lag will be coming from this line:
Your right about doing it asyncrhonously.
You could also scale the image.
What you'll want to do is:
You could set the image to a spinner whilst it is loading.
An example of something similar is:
http://blog.blundellapps.co.uk/imageview-with-loading-spinner/
And an example of image scaling is:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap
Bitmap coverBitmap = BitmapFactory.decodeFile(cursor.getString(holder.column3));Your right about doing it asyncrhonously.
You could also scale the image.
What you'll want to do is:
- spawn a new thread
- use a handler for when the bitmap has decoded
- set the bitmap on the imageview in the callback
You could set the image to a spinner whilst it is loading.
An example of something similar is:
http://blog.blundellapps.co.uk/imageview-with-loading-spinner/
And an example of image scaling is:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap
Code Snippets
Bitmap coverBitmap = BitmapFactory.decodeFile(cursor.getString(holder.column3));Context
StackExchange Code Review Q#12615, answer score: 2
Revisions (0)
No revisions yet.