前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >等比例缩放图片

等比例缩放图片

作者头像
xiangzhihong
发布2018-01-26 17:59:30
2.8K0
发布2018-01-26 17:59:30
举报
文章被收录于专栏:向治洪

/ 缩放图片

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;

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-12-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档