android 中图片的压缩处理
2014-08-20 00:21
204 查看
Android中图片问题的处理(一)
(要加载的图片在本地)
1.如何更有效的加载大图片
每个应用程序所能利用的运行内存空间是有限的(16M),所以在加载大图片是一定要提防内存溢出问题
例如,你加载了一张2592*1936像素的图片,如果这个图片的类型是ARGB_8888,那么加载这张图片所需要的内存
大小是19M,已经超出了所允许的16M的大小
常用图片类型:RGB_565 16位(2个字节)
ARGB_4444 (2个字节)
ARGB_8888 (4个字节),,这些是每个像素所占用的大小
如果在你的应用中要加载的图片是用手机的照相机拍下的,这时候需要注意,很有可能导致内存溢出(并非一定)
最好的办法就是将图片进行等比例缩放(不会带来任何坏处)
步骤:
(要加载的图片在本地)
1.如何更有效的加载大图片
每个应用程序所能利用的运行内存空间是有限的(16M),所以在加载大图片是一定要提防内存溢出问题
例如,你加载了一张2592*1936像素的图片,如果这个图片的类型是ARGB_8888,那么加载这张图片所需要的内存
大小是19M,已经超出了所允许的16M的大小
常用图片类型:RGB_565 16位(2个字节)
ARGB_4444 (2个字节)
ARGB_8888 (4个字节),,这些是每个像素所占用的大小
如果在你的应用中要加载的图片是用手机的照相机拍下的,这时候需要注意,很有可能导致内存溢出(并非一定)
最好的办法就是将图片进行等比例缩放(不会带来任何坏处)
步骤:
1. 得到图片的类型和规模 BitmapFactory.Options options = new BitmapFactory.Options(); //首先将该参数设置为true,避免在加载图片时,为给图片分配内存,也就是说,设置为true时,虚拟机是 //不会给该图片分配内存的,所以,得到的图片时null,这段代码并非要的到图片本身,他只是简单 //的获取该图片的规模,(高度,宽度,类型) options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType; 2。加载一个缩放版本的图片到内存 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) { // Calculate ratios of height and width to requested height and width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } public static 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.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); }
相关文章推荐
- Android BitmapFactory图片压缩处理(大位图二次采样压缩处理)
- Android开发之图片处理专题(二):利用AsyncTask和回调接口实现图片的异步加载和压缩
- Android图片处理-图片压缩处理
- Android图片质量压缩和尺寸压缩处理
- Android中处理大图片时图片压缩
- Android bitmap 图片缩放,压缩处理
- Android中处理大图片时图片压缩
- Android进阶处理图片之压缩图片大小、Lru内存解决方案
- Android图片压缩处理
- android 图片处理(压缩与缓存)
- 比较靠谱的Android图片压缩处理机制,防止OOM
- android 图片过大如何处理?压缩后图片有多大? 分辨率压缩、质量压缩
- android之资源优化处理---图片压缩
- Android中处理大图片时图片压缩
- android下图片压缩 -- 图像处理
- android图片的压缩处理,防止出现OOM(out of memory)
- Android 中图片压缩处理的方式(附源代码)
- Android中3种图片压缩处理方法
- Android 对本地图片进行压缩处理
- 比较靠谱的Android图片压缩处理机制,防止OOM