将bitmap按照centercrop的方式截取

来源:互联网 发布:ogame 源码 编辑:程序博客网 时间:2024/06/05 10:26
将已经加载得到的bitma以一定比例截取图片,newWidth,
newHeight是想要得到图片的大小,可以传入屏幕的宽高
public static Bitmap zoomImg(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;}
0 0