2016-09-04 12 views
2

私は自分のフォルダから写真のリストを取得して1ページに表示するギャラリーアプリケーションを作成したいと考えています。Android Gallery App Loading非常に遅い

イメージの解像度が低下したため、ギャラリーの読み込み速度は向上しましたが、読み込みに時間がかかりすぎました。

私は問題は、すべてのファイルがアプリケーションが開かれるたびに読み込まれていると思います。これをどうすれば解決できますか?

私はアプリでGridviewを使用しました。

マイコード:私のCustomGridViewで

final GridView[] grid = new GridView[1]; 
      final File[] files = fileOps.getGoodFiles(); 
      //this will return the array of files (images) to be displayed 

      final CustomGrid adapter = new CustomGrid(GeoGallery.this,files); 
      grid[0] =(GridView)findViewById(R.id.grid); 
      grid[0].setAdapter(adapter); 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View grid; 
    LayoutInflater inflater = (LayoutInflater) mContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    grid = inflater.inflate(R.layout.grid_single, null); 
    ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image); 
    Bitmap bitmap = BitmapFactory.decodeFile(files[position].getPath()); 
    bitmap = Bitmap.createScaledBitmap(bitmap,120,120*bitmap.getHeight()/bitmap.getWidth(),false); 
    imageView.setImageBitmap(bitmap); 

    return grid; 
} 

どのように私はそれがより速くロードすることができますか?

答えて

1

1)RecyclerViewでGridLayoutManagerを使用する必要があります。 Hereあなたは何か助けを得ることができます。

2)私が間違っていない場合は、完全な画像を読み込んで圧縮しています。 BitmapFactory.Optionsを使用して圧縮イメージを読み込む必要があります。

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 8; 
Bitmap b = BitmapFactory.decodeFile(filepath[position], options); 

3)画像の読み込みや操作のような操作は非常に高価です。したがって、あなたのonBindViewHolderメソッドのスレッドに入れてください

final int pos = position; 
Thread thread = new Thread() { 
    @Override 
    public void run() { 
      try { 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 4; 
       Bitmap b = BitmapFactory.decodeFile(filepath[pos], options); 

       holder.imageView.setImageBitmap(b) 

      } catch (Exception e) 
     } 
    } 
}; 

これは、ギャラリーのパフォーマンスを向上させます。

+0

うわー、ありがとうございました。本当に役に立ちました。 – Grovile