andorid控制代码new 出的 imageview宽高

用xml很好实现,但是项目中要用到动态new出Imageview,所以用代码直接new Imageview。用了好多方法没有实现,如:

 ImageView photo=new ImageView(mContext);
	        RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(100,200);
	        photo.setLayoutParams(param);

暂时解决不了,用了一种替代方法:把imageview里面的bitmap进行缩放到固定长宽:

	// 得到图片原始的高宽 
				    	int rawHeight = tmpBitmap.getHeight(); 
				    	int rawWidth = tmpBitmap.getWidth(); 
				    	// 设定图片新的高宽 
				    	int newHeight = 200; 
				    	int newWidth = 100; 
				    	// 计算缩放因子 
				    	float heightScale = ((float) newHeight) / rawHeight; 
				    	float widthScale = ((float) newWidth) / rawWidth; 
				    	// 新建立矩阵 
				    	Matrix matrix = new Matrix(); 
				    	matrix.postScale(heightScale, widthScale); 
				    	 Bitmap resizeBmp =Bitmap.createBitmap(tmpBitmap,0,0,rawWidth,rawHeight,matrix,true);


resizeBmp是就是缩放后的图,效果实现还可以。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值