2013-11-02 71 views
0

我爲我的圖像製作了一個數組,以在GridView列表中顯示它們。問題在於它佔用大量內存,滾動緩慢(不平滑)並且每次都會關閉。在我檢查解決方案後,我看到我需要將它們保存到存儲中,然後在應用程序中使用它們。但它不起作用。有人可以幫我怎麼做嗎?如何將我的GridView陣列圖像保存到存儲?

這是我的適配器代碼:

public class ImageAdapter extends BaseAdapter { 
Context mContext; 
// Keep all Images in array 
public Integer[] mThumbIds = { R.drawable.d, R.drawable.t, 
     R.drawable.e, R.drawable.e, R.drawable.r, 
     R.drawable.asdt, R.drawable.asd, R.drawable.a, 
     //... etc 

}; 

public static int calculateInSampleSize(BitmapFactory.Options options, 
     int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and 
     // keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
     int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize=4; 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, 
      reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 

// Constructor 
public ImageAdapter(Context c) { 
    mContext = c; 
} 

@Override 
public int getCount() { 
    return mThumbIds.length; 
} 

@Override 
public Object getItem(int position) { 
    return mThumbIds[position]; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ImageView imageView; 
    imageView = new ImageView(mContext); 

    imageView.setImageBitmap(decodeSampledBitmapFromResource(
      imageView.getResources(), mThumbIds[position], 100, 100)); 
    imageView.buildDrawingCache(isEmpty()); 
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
    imageView.setLayoutParams(new GridView.LayoutParams(
      (getScreenWidth()/3), (getScreenWidth()/3))); 
    return imageView; 

} 

@SuppressWarnings("deprecation") 
@SuppressLint("NewApi") 
public int getScreenWidth() { 
    int columnWidth; 
    WindowManager wm = (WindowManager) mContext 
      .getSystemService(Context.WINDOW_SERVICE); 
    Display display = wm.getDefaultDisplay(); 

    final Point point = new Point(); 
    try { 
     display.getSize(point); 
    } catch (java.lang.NoSuchMethodError ignore) { // Older device 
     point.x = display.getWidth(); 
     point.y = display.getHeight(); 
    } 
    columnWidth = point.x; 
    return columnWidth; 
} 

}

回答

0

我只是用Picasso,讓它做所有的工作對我來說。

Picasso.with(context).load(R.drawable.whatever).into(imageView); 

並完成。

+0

嘿謝謝,但你能告訴我在我的應用程序中使用它? –

+0

in getView()而不是setImageBitmap。如果需要節省存儲空間,則可以使用該庫調整圖像大小。 –