android 按尺寸大小比例裁剪 得到中间部分图片 缩放

public static Bitmap zoomBitmap(Bitmap bitmap, float vw, float vh) {
    float width = bitmap.getWidth();//获得图片宽高
    float height = bitmap.getHeight();

    float scaleWidht, scaleHeight, x, y;//图片缩放倍数以及x,y轴平移位置
    Bitmap newbmp = null; //新的图片
    Matrix matrix = new Matrix();//变换矩阵
    if ((width/height)<=vw/vh){//当宽高比大于所需要尺寸的宽高比时以宽的倍数为缩放倍数
        scaleWidht = vw / width;
        scaleHeight = scaleWidht;
        y = ((height*scaleHeight - vh) / 2)/scaleHeight;// 获取bitmap源文件中y做表需要偏移的像数大小
        x = 0;
    }else {
        scaleWidht = vh / height;
        scaleHeight = scaleWidht;
        x = ((width*scaleWidht -vw ) / 2)/scaleWidht;// 获取bitmap源文件中x做表需要偏移的像数大小
        y = 0;
    }
    matrix.postScale(scaleWidht / 1f, scaleHeight / 1f);
    try {
        if (width - x > 0 && height - y > 0&&bitmap!=null)//获得新的图片 (原图,x轴起始位置,y轴起始位置,x轴结束位置,Y轴结束位置,缩放矩阵,是否过滤原图)为防止报错取绝对值
            newbmp = Bitmap.createBitmap(bitmap, (int) Math.abs(x), (int) Math.abs(y), (int) Math.abs(width - x*2),
                    (int) Math.abs(height - y*2), matrix, false);// createBitmap()方法中定义的参数x+width要小于或等于bitmap.getWidth(),y+height要小于或等于bitmap.getHeight()
    } catch (Exception e) {//如果报错则返回原图,不至于为空白
        e.printStackTrace();
        return bitmap;
    }
    return newbmp;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值