如何優化我的代碼以加載圖像快速閃爍?我的意思是在快速上下滾動後,需要幾秒或更長時間才能將圖像加載到我的ListView
的ImageView
中。這裏是我的適配器我的示例代碼:使用AsyncTask加載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));
}
}
}
'1.'我不能做我自己的目標是GB。 '2.'我會試試這個並且回覆你。 '3.'有沒有我可以做的任何優化讓它更快?我的意思是在現有的代碼中。而不是緩存。我知道的緩存會讓它更快。 '4.'我可以使用HashMap而不是LRU緩存來使東西更容易嗎? – h4ck3d 2013-02-24 17:59:39
1. http://android-developers.blogspot.co.uk/2010/07/how-to-have-your-cupcake-and-eat-it-too.html 2.好的3.不是我能看到的無需自己構建並進行更密切的調試。 4.不需要使用LruCache。一個HashMap最終會使您的應用程序崩潰,並出現OutOfMemory錯誤。但請相信我,LruCache非常簡單,只需在幻燈片上看到的那幾行就可以在4:30 – Budius 2013-02-24 18:14:38
LRU完成這項工作。 – h4ck3d 2013-02-25 22:22:04