当Bitmap的宽度大于ImageView的最大显示宽度时对ImageView的宽高重新计算来适应Bitmap的宽高...

当bitmap的宽大于ImageView显示控件的宽度时,如果不对ImageView的宽高重新计算,那么Imageview的高度就会超过显示图片的高度,用户体验将会很差,所以我们需要重新计算显示控件的宽高,这里我们假定ImageView的最大宽度为屏幕的宽度。利用下面这个工具类就可以很好解决问题了:

public static LinearLayout.LayoutParams getLayoutParams(Bitmap bitmap,int screenWidth){
		
		float rawWidth = bitmap.getWidth();
		float rawHeight = bitmap.getHeight();
		
		float width = 0;
		float height = 0;

		Log.i("hello", "原始图片高度:"+rawHeight+"原始图片宽度:"+rawWidth);
		Log.i("hello", "原始高宽比:"+(rawHeight/rawWidth));
		
		if(rawWidth > screenWidth){
			height = (rawHeight/rawWidth)*screenWidth;
			width = screenWidth;
		}else{
			width = rawWidth;
			height = rawHeight;
		}
		
		Log.i("hello", "处理后图片高度:"+height+"处理后图片宽度:"+width);
		
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)width, (int)height);
		
		return layoutParams;
	}

将得到的LayoutParams直接设置给显示控件就ok了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值