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; }
android 按尺寸大小比例裁剪 得到中间部分图片 缩放
最新推荐文章于 2024-04-24 15:15:43 发布