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

本文介绍了一个Java方法,用于加载本地图片并实现等比缩放,确保图片在不同设备上显示清晰不失真。
/**
	 * 利用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;
		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值