android Bitmap处理以 节省内存的方法
2012-11-14 15:10
267 查看
/** *获取尺寸为maxWidth*maxHeight大小的图片 maxWidth maxHeight单位为像素px *android布局文件一般都是用与像素无关的dp为单位 *可以使用 dip2px 方法转dp为dx */ public static Bitmap change_image(String ipath, int maxWidth, int maxHeight) { BitmapFactory.Options opts1 = new Options(); opts1.inJustDecodeBounds = true; BitmapFactory.decodeFile(Uri.parse(ipath).toString(), opts1); int size = computeSampleSize(opts1, -1, maxWidth, maxHeight); BitmapFactory.Options opts = new Options(); opts.inSampleSize = size; System.out.println("ld opts.inSampleSize=" + opts.inSampleSize); opts.inJustDecodeBounds = false; Bitmap btm = BitmapFactory .decodeFile(Uri.parse(ipath).toString(), opts); return btm; } /** * 将图片裁剪到合适大小 */ public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxWidth, int maxHeight) { int pW = options.outWidth; int pH = options.outHeight; if (pW <= maxWidth && pH <= maxHeight) { return 1; } int roundedSize = pW / maxWidth; if (pW % maxWidth != 0) { roundedSize += 1; } int r1 = pH / maxHeight; if (pH % maxHeight != 0) { r1 += 1; } roundedSize = Math.max(r1, roundedSize); return roundedSize; } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
相关文章推荐
- 5个Android常见内存泄露及处理方法
- Android处理Bitmap的一些方法
- Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法
- Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法
- Android中常用的bitmap处理方法
- android bitmap 指定任何一个角为圆角 处理方法
- 在android中创建bitmap避免内存不足的方法
- Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法
- Android 异步获取网络图片并处理导致内存溢出问题解决方法
- Android处理Bitmap的一些方法(多)
- android 常用Bitmap处理方法收集:普通裁剪,缩放,圆形裁剪
- android 内存溢出处理办法(出现java.lang.OutOfMemoryError: bitmap size exceeds VM budget 异常)
- Android Bitmap图片处理,防止内存溢出
- Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法
- android bitmap内存处理
- Android进阶图片处理之三级缓存图片框架BitmapUtil的使用方法
- Android 处理内存泄露的方法
- Android图片处理总结五:Bitmap方法总结
- Android 异步获取网络图片并处理导致内存溢出问题解决方法
- Android中常用的bitmap处理方法 (bitmap工具类)