首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android将图片转化作指定的宽和高

2012-08-29 
android将图片转化为指定的宽和高需求:开发中服务器端要求把获取到的图片压缩处理,转化为指定的宽和高,例

android将图片转化为指定的宽和高

    需求:开发中服务器端要求把获取到的图片压缩处理,转化为指定的宽和高,例如:需要上传宽100,高200的图片

    在android2.2提供了一个API可以直接实现

/** *  处理图片  * @param bm 所要转换的bitmap * @param newWidth新的宽 * @param newHeight新的高   * @return 指定宽高的bitmap */ 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;  } 



1楼smileroy4天前 15:13
谢谢楼主。
Re: walker02昨天 19:16
回复smileroy 共同学习

热点排行