不解释大家懂得,在listview 或grid或viewpager等大量大尺寸图片时,会造成OOM

这里是优化图片内存的一个方法,注释写的很 明确..

public Bitmap getBitmapFromNet(final String url,final int width,final int height){//从网络下载图片
try {

//图片内存算法
//width*height*Config(480*320*ARGB_8888)——>480*320*4 byte

BitmapFactory.Options opt = new BitmapFactory.Options();

/*组合使用*/
opt.inPurgeable = true;
opt.inInputShareable = true;

opt.inPreferredConfig = Bitmap.Config.RGB_565;

opt.outWidth = width;
opt.outHeight = height;
opt.inJustDecodeBounds = true;//设置为true将不会返回真正大小

int yRatio = (int)Math.ceil(opt.outHeight/height);
int xRatio = (int)Math.ceil(opt.outWidth/width);

if (yRatio > 1 || xRatio > 1){
if (yRatio > xRatio) {
opt.inSampleSize = yRatio; //缩放值
}
else {
opt.inSampleSize = xRatio;
}
}



Bitmap bitmap = BitmapFactory.decodeStream(new URL(url).openStream(),null,opt);
//bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
cache.put(url, new SoftReference<Bitmap>(bitmap));//保存缓存--------->缓存Map
return bitmap;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

本文链接

更多相关文章

  1. android大图片显示
  2. Android入门第十二篇之Gallery
  3. Android技术积累:图片缓存管理
  4. 更换android的初始化图片
  5. Android(安卓)开发注意事项
  6. 【转】【Android】获取手机中已安装apk文件信息(PackageInfo、Re
  7. android中Logcat的深层理解
  8. Android使用ImageLoader异步加载网络图片(二)结合listview
  9. Android图片放大缩小实现方式(一)

随机推荐

  1. Android(安卓)-- Vibrator
  2. android 是什么
  3. Android的常见错误及解决办法
  4. Unity 与 Android (Android Studio)的交
  5. android中android:wight详解
  6. 横屏和竖屏切换不用销毁重新建立android:
  7. android 9.0系统重启的代码详解
  8. Android内核源码阅读---ashmem.c
  9. Android的一些常用命令提示符(cmd)指令
  10. Android中使用SAX方式解析XML文件