您的位置:首页 > 移动开发 > Android开发

android 利用ImagevView加载本地图片并实现等比缩放

2013-01-31 10:55 846 查看
/**
* 利用ImagevView加载本地图片并实现等比缩放
*
* @param url
* @param maxX
* @param maxY
* @return
*/
public static Bitmap zoomLocalBitmap(String url, int maxX, int maxY) {
float scale;
try {
FileInputStream fis = new FileInputStream(url);
Bitmap bitmap = BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片
int imgWidth = bitmap.getWidth();
int imgHeight = bitmap.getHeight();
if (imgWidth > maxX || imgHeight > maxY) {

float scaleWidth = ((float) maxX) / imgWidth;
float scaleHeight = ((float) maxY) / imgHeight;
if (scaleWidth > scaleHeight) {
scale = scaleWidth;
} else {
scale = scaleHeight;
}
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, imgWidth,
imgHeight, matrix, true);
return newBitmap;
} else
return bitmap;

} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: