2010-11-25 7 views
1

私のアプリには、ユーザーのSDカードの画像をオプションで含むことができるリストビューが含まれています。しかし、画像がロードされているときは目立つ遅れがあります。私はこれを私のgetViewに持っています:Android listview image lag

  BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(image, o); 
      final int REQUIRED_SIZE=70; 

      //Find the correct scale value. It should be the power of 2. 
      int width_tmp=o.outWidth, height_tmp=o.outHeight; 
      int scale=1; 
      while(true){ 
       if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
        break; 
       width_tmp/=2; 
       height_tmp/=2; 
       scale++; 
      } 

      //Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize=scale; 
      Bitmap myBitmap = BitmapFactory.decodeFile(image, o2); 
      image_main.setImageBitmap(myBitmap); 

何か提案がありますか?

EDIT:これで上記置き換えが、何の画像がロードされていない...

  class Thumbnailer extends AsyncTask<String, Void, Bitmap> { 
        String image; 

        @Override 
        protected void onPostExecute(Bitmap result) { 
         image_main.setImageBitmap(result); 
        } 
        @Override 
        protected void onProgressUpdate(Void... progress) { 
        } 

        @Override 
        protected Bitmap doInBackground(String... params) { 
         BitmapFactory.Options o = new BitmapFactory.Options(); 
         o.inJustDecodeBounds = true; 
         BitmapFactory.decodeFile(image, o); 
         final int REQUIRED_SIZE=70; 

         //Find the correct scale value. It should be the power of 2. 
         int width_tmp=o.outWidth, height_tmp=o.outHeight; 
         int scale=1; 
         while(true){ 
          if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
           break; 
          width_tmp/=2; 
          height_tmp/=2; 
          scale++; 
         } 
         //Decode with inSampleSize 
         BitmapFactory.Options o2 = new BitmapFactory.Options(); 
         o2.inSampleSize=scale;         
         return BitmapFactory.decodeFile(image, o2); 
        } 
       } 
       new Thumbnailer().execute(image); 

答えて

2

はプレースホルダ画像と行を表示します。 BitmapFactoryAsyncTaskで動作し、完了したらImageViewを更新します(ImageViewがリサイクルされておらず、まだこの特定のサムネイルが必要な場合)。画像を変更しない場合は、少なくともListViewのこの化身については、すべてのスケーリングをやり直す必要がないように作業をキャッシュしてください。

+0

レスポンスありがとうございます。私はAsyncTaskを試していますが、動作しません。私が行ったことを示すために私の第1のポストを更新しました。私はAsyncTaskを使いこなしたことがないので、間違っているかもしれません。私はここでかなり初心者です。) – Paul

+0

@Paul: "働かせられない"ということを言いたくない人を助けるのは難しいです。私が知る限りでは、すべての画像を単一の 'ImageView'ウィジェットに読み込もうとしていますが、そうは思われません。この特定のタスクで処理している行の 'ImageView'を' AsyncTask'に渡す必要があります。 – CommonsWare

+0

問題は私が実際にイメージ文字列を使用していないと思いますinBackgroundに渡しています – Paul