Android图像处理
相机的拍摄照片尺寸通常都很大,需要预先处理,然后才能在ImageView视图上显示。主流Android手机都带有800万像素的照相机组件。大尺寸的图片很容易耗尽应用的内存。因此,加载图片前,需要编写代码缩小图片。图片使用完毕,也需要编写代码清理删除它。
/** * 图像处理 * @author mw * */ public class PictureUtils { /** * 将图片缩放到设备默认的显示尺寸 onStart()调用 * @param a * @param path * @return */ public static BitmapDrawable getScaledDrawable(Activity a,String path){ Display display = a.getWindowManager().getDefaultDisplay(); float destWidth = display.getWidth(); float destHeight = display.getHeight(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path,options); float srcWidth = options.outWidth; float srcHeight = options.outHeight; int inSampleSize = 1; if (srcWidth > destWidth || srcHeight > destHeight) { if (srcWidth > srcHeight) { inSampleSize = Math.round(srcWidth/ destWidth); }else{ inSampleSize = Math.round(srcHeight / destHeight); } } options = new BitmapFactory.Options(); options.inSampleSize = inSampleSize; Bitmap bitmap = BitmapFactory.decodeFile(path,options); return new BitmapDrawable(a.getResources(),bitmap); } /** * 缷载图片 onStop()调用 * @param imageView */ public static void cleanImageView(ImageView imageView){ if ( !(imageView.getDrawable() instanceof BitmapDrawable)) { return; } BitmapDrawable b = (BitmapDrawable)imageView.getDrawable(); //释放bitmap占用的原始存储空间,避免可能的内存耗尽问题 b.getBitmap().recycle(); imageView.setImageDrawable(null); } }//以下示例:加载图片---压缩图片---缩放图片---处理圆角
/** * 根据传入的宽和高,计算出合适的inSampleSize值 * @param options * @param reqWidth * @param reqHeight * @return */ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // 源图片的高度和宽度 final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // 计算出实际宽高和目标宽高的比率 if (width > height) { inSampleSize = Math.round((float) width / (float) reqWidth); }else { //round 返回最接近参数,意思也就是四舍五入math.round(-8.1)=-8 math.round(8.9)=9 inSampleSize = Math.round((float) height / (float) reqHeight); } // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高 // 一定都会大于等于目标的宽和高。 //inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } /** * 压缩好的bitmap * @param pathName * @param reqWidth * @param reqHeight * @return */ public static Bitmap decodeSampledBitmapFromResource(String pathName, int reqWidth, int reqHeight) { // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小 final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathName,options); // 调用上面定义的方法计算inSampleSize值 options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight); // 使用获取到的inSampleSize值再次解析图片 options.inJustDecodeBounds = false; //options.inSampleSize = 5; return BitmapFactory.decodeFile(pathName,options); } /** * 处理圆角的图标 * @param background * @param src * @param r 弧度 * @param x * @param y * @return */ public static Bitmap mergerIcon(Bitmap background,Bitmap src,int r,int x,int y){ Bitmap output = Bitmap.createBitmap(background.getWidth(), background.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff000000; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, background.getWidth(), background.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = r; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(background, rect, rect, paint); // canvas.drawBitmap(src, rect, rect, paint); canvas.drawBitmap(src, x, y, paint); canvas.save(); return output; } /**缩放图片*/ public static Bitmap resizeImage(Bitmap src, int destW, int destH){ if(src == null)return null; int srcW = src.getWidth(); int srcH = src.getHeight(); float scaleWidth = 1; float scaleHeight = 1; if(srcW == destW&&srcH == destH){ return src; } //计算出这次要缩小的比例 scaleWidth=(float)destW/(float)srcW; scaleHeight=(float)destH/(float)srcH; //产生resize后的Bitmap对象 Matrix matrix=new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp=Bitmap.createBitmap(src, 0, 0, srcW, srcH, matrix, true); return resizeBmp; }//示例使用
//获取图片路径 File fileDir = new File(SdcardUtil.getSdcardCollInfo() + str + "/" + pathList.get(0)); //处理图片 Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromResource(fileDir.getPath(),200,700); Bitmap background = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.intdex_page_cll_point); //缩放图片 Bitmap map = BitmapUtils.resizeImage(bitmap, background.getWidth(), background.getWidth()); //处理圆角 image_Bitemap.setImageBitmap(BitmapUtils.mergerIcon(background, map, 7,0,0));