建议使用如下的这种,应用了LruCache作为管理
- public class ImageUtil { private LruCache<String, Bitmap> mMemoryCache;
- private final Context mContext; private static ImageUtil imageUtil;
- private static Object obj = new Object(); private int memClass;
- private int cacheSize; private ImageUtil(Context mContext) {
- this.mContext = mContext; createLruCache(mContext);
- } private void createLruCache(Context mContext) {
- memClass = ((ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass(); cacheSize = 1024 * 1024 * memClass / 8;
- mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override
- protected int sizeOf(String key, Bitmap value) { // TODO Auto-generated method stub
- return value.getRowBytes(); }
- }; }
- public static ImageUtil getInstance(Context mContext) { if (imageUtil == null) {
- synchronized (obj) { if (imageUtil == null) {
- imageUtil = new ImageUtil(mContext); }
- } }
- return imageUtil; }
- public void adjustImageSize(ImageView imageView, int imageResourceId) { Bitmap mBitmap = null;
- Display display = ((MainActivity) mContext).getWindowManager().getDefaultDisplay(); int width = display.getWidth(); // deprecated
- int height = display.getHeight(); // deprecated Bitmap bitmapCache = mMemoryCache.get(imageResourceId + "");
- if (bitmapCache != null) { mBitmap = bitmapCache;
- } else { mBitmap = createImageWithResouce(mContext, imageResourceId);
- mMemoryCache.put(imageResourceId + "", mBitmap); }
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, width / getBitmapWidth(mBitmap) * getBitmapHeight(mBitmap));










