等比例缩放图片

简介: / 缩放图片public static Bitmap zoomImg(String img, int newWidth ,int newHeight){// 图片源   Bitmap bm = BitmapFactory.decodeFile(img);   if(null!=bm){    return zoomImg(bm,newWidth,newHeight);   }   retu
/ 缩放图片
public static Bitmap zoomImg(String img, int newWidth ,int newHeight){
// 图片源
   Bitmap bm = BitmapFactory.decodeFile(img);
   if(null!=bm){
    return zoomImg(bm,newWidth,newHeight);
   }
   return null;
}

public static Bitmap zoomImg(Context context,String img, int newWidth ,int newHeight){
// 图片源
try {
Bitmap bm = BitmapFactory.decodeStream(context.getAssets()
.open(img));
if (null != bm) {
return zoomImg(bm, newWidth, newHeight);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 缩放图片
public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){
   // 获得图片的宽高
   int width = bm.getWidth();
   int height = bm.getHeight();
   // 计算缩放比例
   float scaleWidth = ((float) newWidth) / width;
   float scaleHeight = ((float) newHeight) / height;
   // 取得想要缩放的matrix参数
   Matrix matrix = new Matrix();
   matrix.postScale(scaleWidth, scaleHeight);
   // 得到新的图片
   Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
    return newbm;
}
目录
相关文章
|
C# 数据安全/隐私保护
等比例缩放图片
/ 缩放图片<br><span style="white-space:pre"></span>public static Bitmap zoomImg(String img, int newWidth ,int newHeight){<br><span style="white-space:pre"></span>// 图片源<br><span style="white-space:pre
1217 0
|
Java 测试技术 BI
ImageUtil 图片的缩放 裁剪
      import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.
1040 0
|
JavaScript 前端开发
|
JavaScript
问题解决:百分比宽度页面缩放会变形
问题解决:百分比宽度页面缩放会变形
253 1
问题解决:百分比宽度页面缩放会变形

热门文章

最新文章