0

コードを最適化して画像をすばやく読み込むにはどうすればよいですか?つまり、高速で上下にスクロールした後、画像をのにロードするのに数秒以上かかりImageViewます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));
            }
        }
    }
4

2 に答える 2

1

私が考えることができる2つのことがあります:

  1. ICS以降、AsyncTaskはシングルスレッドのものです。つまり、10個のAsyncTaskを起動すると、1番目を完了し、次に2番目、次に3番目に進み、他のタスクが完了するのを常に待ってから続行します。この.executeOnExecutor方法を使用して、より多くのスレッドと並行してタスクを実行できます。

  2. LruCacheを使用して、画像のRAMキャッシュを実行します。Google IO 2012のこのビデオは、LruCacheの作成方法を正確に示しています(クールなトリックがたくさんあるので、ビデオ全体を見るように常に勧めています)

于 2013-02-24T17:53:03.007 に答える
0

互換性ライブラリを試してみてくださいBitmapFun.zipAndroid2.3でうまく機能します!

または、互換性ライブラリが必要ない場合は、(古いバージョン)を試すことができますImageDownloader

于 2013-02-24T18:06:02.167 に答える