Android 使用AsyncTask在ListView中加载图像有点慢

Android 使用AsyncTask在ListView中加载图像有点慢,android,performance,optimization,android-listview,android-imageview,Android,Performance,Optimization,Android Listview,Android Imageview,如何优化代码以快速加载图像?我的意思是,快速上下滚动后,需要几秒钟或更长时间才能将图像加载到我的列表视图的图像视图中。以下是适配器的示例代码: public void bindView(View view, Context context, Cursor cursor) { String title = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)); String al

如何优化代码以快速加载图像?我的意思是,快速上下滚动后,需要几秒钟或更长时间才能将图像加载到我的
列表视图的
图像视图中。以下是适配器的示例代码:

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));
            }
        }
    }
public void bindView(视图、上下文、光标){
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(标题);
Uri sArtworkUri=Uri.parse(“content://media/external/audio/albumart");
Uri=ContentUris.withAppendedId(sArtworkUri,Integer.valueOf(album_id));
iv.setTag(uri);
iv.setImageResource(R.drawable.background_holo_dark);
新的MyImageLoader(上下文、视图、iv、uri);
}
私有类MyImageLoader扩展了异步任务{
语境;
观点五;
ImageView iv;
乌里乌;
MyImageLoader(上下文上下文、视图v、图像视图iv、Uri u){
this.context=上下文;
这个,v=v;
这4.iv=iv;
这个。u=u;
}
受保护的同步位图doInBackground(Uri…参数){
ContentResolver res=context.getContentResolver();
InputStream in=null;
试一试{
in=res.openInputStream(参数[0]);
} 
catch(filenotfounde异常){
e、 printStackTrace();
}
位图artwork=BitmapFactory.decodeStream(in);
归还艺术品;
}
受保护的void onPostExecute(位图bmp){
如果(bmp!=null)
{ImageView iv=(ImageView)v.findViewById(R.id.imgIcon);
if(iv.getTag().toString().equals(u.toString()))
iv.设置图像位图(bmp);
//iv.setImageBitmap(Bitmap.createScaledBitmap(bmp,100100,false));
}
}
}

我可以想到两件事:

  • 从ICS开始,异步任务是单线程的,这意味着,如果启动10个异步任务,它将完成第一个,然后转到第二个,然后是第三个,始终等待其他任务完成后再继续。您可以使用它的
    .executeOnExecutor
    方法来与更多线程并行运行任务

  • 使用LruCache对图像进行RAM缓存。这正好说明了如何制作LruCache(我总是建议人们观看整个视频,因为有很多很酷的技巧)

  • 试用兼容性库在Android 2.3上非常有用

    或者,如果您不想使用兼容库,可以尝试(旧版本)
    ImageDownloader


    1.
    我不能这样做,因为我的目标是GB<代码>2。
    我会试试这个,然后再给你回复<代码>3。
    我可以进行任何优化,使之更快吗?我的意思是在现有的代码中。而不是缓存。我知道缓存会让它更快<代码>4。我可以使用HashMap而不是LRU缓存来简化工作吗?1。2.好的。如果不亲自构建它并进行更仔细的调试,我就看不到这一点。4.不,用LruCache。HashMap最终会导致应用程序崩溃,并出现OutOfMemory错误。但是相信我,LruCache是非常简单的,仅仅是你在4:30的幻灯片上看到的那几行