android bitmap等比缩放,等比缩放Bitmap至新宽高并裁剪掉额外部分

这段代码是一个用于将图片按比例缩放的Java方法。它首先计算原始图片与目标尺寸的比例,然后根据比例确定新的宽度和高度。如果原始图片较宽,则从左上角裁剪以获取正方形;反之,如果较窄,从顶部裁剪。最后,使用`Bitmap.createBitmap`创建新的缩放后的Bitmap对象,并回收原始资源。
摘要由CSDN通过智能技术生成

private static Bitmap createScaledBitmap(Bitmap bm, int newWidth, int newHeight) {

int w = bm.getWidth(); // 得到图片的宽,高

int h = bm.getHeight();

int retX;

int retY;

double wh = (double) w / (double) h;

double nwh = (double) newWidth / (double) newHeight;

if (wh > nwh) {

retX = h * newWidth / newHeight;

retY = h;

} else {

retX = w;

retY = w * newHeight / newWidth;

}

int startX = w > retX ? (w - retX) / 2 : 0;//基于原图,取正方形左上角x坐标

int startY = h > retY ? (h - retY) / 2 : 0;

Bitmap bit = Bitmap.createBitmap(bm, startX, startY, retX, retY, null, false);

bm.recycle();

return bit;

}

来源网络,原作者未知,侵权请告知备注原作者姓名或删除。

private static Bitmap createScaledBitmap(Bitmap bm, int newWidth, int newHeight) {

int w = bm.getWidth(); // 得到图片的宽,高

int h = bm.getHeight();

int retX;

int retY;

double wh = (double) w / (double) h;

double nwh = (double) newWidth / (double) newHeight;

if (wh > nwh) {

retX = h * newWidth / newHeight;

retY = h;

} else {

retX = w;

retY = w * newHeight / newWidth;

}

int startX = w > retX ? (w - retX) / 2 : 0;//基于原图,取正方形左上角x坐标

int startY = h > retY ? (h - retY) / 2 : 0;

Bitmap bit = Bitmap.createBitmap(bm, startX, startY, retX, retY, null, false);

bm.recycle();

return bit;

}

来源网络,原作者未知,侵权请告知备注原作者姓名或删除。

private static Bitmap createScaledBitmap(Bitmap bm, int newWidth, int newHeight) {

int w = bm.getWidth(); // 得到图片的宽,高

int h = bm.getHeight();

int retX;

int retY;

double wh = (double) w / (double) h;

double nwh = (double) newWidth / (double) newHeight;

if (wh > nwh) {

retX = h * newWidth / newHeight;

retY = h;

} else {

retX = w;

retY = w * newHeight / newWidth;

}

int startX = w > retX ? (w - retX) / 2 : 0;//基于原图,取正方形左上角x坐标

int startY = h > retY ? (h - retY) / 2 : 0;

Bitmap bit = Bitmap.createBitmap(bm, startX, startY, retX, retY, null, false);

bm.recycle();

return bit;

}

来源网络,原作者未知,侵权请告知备注原作者姓名或删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值