android裁剪缩略图的方法

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值