【问题标题】:Images loading a bit slow in ListView using AsyncTask使用 AsyncTask 在 ListView 中加载图像有点慢
【发布时间】:2013-02-24 17:32:32
【问题描述】:

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

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));
            }
        }
    }

【问题讨论】:

    标签: android performance optimization android-listview android-imageview


    【解决方案1】:

    我能想到两件事:

    1. 在 ICS 中启动 AsyncTask 是一个单线程的事情,这意味着,如果您触发 10 个 AsyncTask,它将完成第一个,然后转到第二个,然后是第三个,始终等待其他人完成后再继续.您可以使用它的.executeOnExecutor 方法来并行运行更多线程的任务。

    2. 使用 LruCache 对图像进行 RAM 缓存。这个video from Google IO 2012 准确地展示了如何制作 LruCache(我总是建议人们观看整个视频,因为有很多很酷的技巧)

    【讨论】:

    • 1. 我不能这样做,因为我的目标是 GB。 2. 我会试试这个并回复你。 3. 有什么优化可以让它更快吗?我的意思是在现有代码中。而不是缓存。我知道缓存会使它更快。 4.我可以使用 HashMap 而不是 LRU 缓存来使事情变得更容易吗?
    • 1. android-developers.blogspot.co.uk/2010/07/… 2. 好的 3. 如果不自己构建它并进行更仔细的调试,我看不到。 4. 不,使用 LruCache。 HashMap 最终会因 OutOfMemory 错误而使您的应用程序崩溃。但请相信我,LruCache 非常简单,就在 4:30 的幻灯片中您可以看到的那几行
    【解决方案2】:

    尝试BitmapFun.zip 与兼容性库在 Android 2.3 上运行良好!

    或者,如果您不想要任何兼容性库,您可以尝试(旧版本)ImageDownloader

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 2012-08-19
      • 2016-01-26
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      相关资源
      最近更新 更多