public static Bitmap corp(Bitmap bitmap) { int corpWith = Configs.CORP_THUMBS_WIDTH; int corpHeight = Configs.CORP_THUMBS_HEIGHT; int width = bitmap.getWidth(); int height = bitmap.getHeight(); int srcLeft = 0; int srcTop = 0; int dstLeft = 0; int dstTop = 0; Bitmap output = Bitmap.createBitmap(corpWith,corpHeight, Config.ARGB_8888); Canvas canvas = new Canvas(output); if(corpWith >= width){ dstLeft = (corpWith -width)/2; corpWith = width; }else{ srcLeft = (width - corpWith)/2; } if(corpHeight >= height){ dstTop = (corpHeight - height)/2; corpHeight = height; }else{ srcTop = (height - corpHeight)/2; } Log.i(Constants.LOG_TAG_DEBUG, "corpWith:" + corpWith + ",corpHeight:" + corpHeight + ",dstLeft:" + dstLeft + ",dstTop:"+dstTop); final int color = 0xff424242; final Paint paint = new Paint(); final Rect dstRect = new Rect(dstLeft, dstTop, corpWith + dstLeft, corpHeight + dstTop); final Rect srcRect = new Rect(srcLeft, srcTop, corpWith + srcLeft, corpHeight + srcTop); final RectF rectF = new RectF(dstRect); final float roundPx = Configs.CORP_THUMBS_ROUND; //圆角像素 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(bitmap, srcRect, dstRect, paint); return output; }
关于Rect 的详细解释如下
Rect dstRect = new Rect(dstLeft, dstTop, corpWith + dstLeft, corpHeight + dstTop);
这个构造方法需要四个参数这四个参数 指明了什么位置 ?我们就来解释怎么画 这个 矩形
这四个 参数 分别代表的意思是:left top right bottom 上下左右呗。啊,不是 是 左 上 右 下。 下面给大家解释
left : 矩形左边的X坐标 150 ---->图片中的A点
top: 矩形顶部的Y坐标 75 ---->图片中的B点
right : 矩形右边的X坐标 260 ----->图片中的C点
bottom: 矩形底部的Y坐标 120 ------->图片中的D点
说白了就是左上角的坐标是(150,75),右下角的坐标是(260,120),这样就好理解了
具体参考 http://byandby.iteye.com/blog/825330