如何优化我的代码以快速加载图像?我的意思是在快速上下滚动后,需要几秒钟或更长时间才能将图像加载到ImageView
my ListView
. 这是我的适配器的示例代码:
public void bindView(View view, Context context, Cursor cursor) {
String title = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE));
String album_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
ImageView iv = (ImageView)view.findViewById(R.id.imgIcon);
TextView text = (TextView)view.findViewById(R.id.txtTitle);
text.setText(title);
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, Integer.valueOf(album_id));
iv.setTag(uri);
iv.setImageResource(R.drawable.background_holo_dark);
new MyImageLoader(context,view,iv,uri).execute(uri);
}
private class MyImageLoader extends AsyncTask<Uri, Void, Bitmap>{
Context context;
View v;
ImageView iv;
Uri u;
MyImageLoader(Context context,View v,ImageView iv,Uri u){
this.context = context;
this.v = v;
this.iv = iv;
this.u = u;
}
protected synchronized Bitmap doInBackground(Uri... param) {
ContentResolver res = context.getContentResolver();
InputStream in = null;
try {
in = res.openInputStream(param[0]);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap artwork = BitmapFactory.decodeStream(in);
return artwork;
}
protected void onPostExecute(Bitmap bmp){
if(bmp!=null)
{ ImageView iv = (ImageView)v.findViewById(R.id.imgIcon);
if(iv.getTag().toString().equals(u.toString()))
iv.setImageBitmap(bmp);
//iv.setImageBitmap(Bitmap.createScaledBitmap(bmp, 100, 100, false));
}
}
}