读书人

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

发布时间: 2012-08-29 08:40:14 作者: rapoo

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 共同学习

读书人网 >Android

热点推荐