图片宽度为控件宽度,高度按比例缩放

来源:互联网 发布:mmd舞蹈动作数据 编辑:程序博客网 时间:2024/04/30 13:58

ImageView图片宽度为控件宽度,高度按比例缩放

图片宽度固定,高度按比例缩放自适应

本身不知道图片宽度和高度


 首先,定义ImageView,在该ImageView中,我们需要设置属性android:adjustViewBounds="true",他的意思图片是否保持宽高比。切记的一点是该属性需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。 

 

[html] view plain copy
  1. <ImageView  
  2.        android:id="@+id/img_list"  
  3.        android:layout_width="fill_parent"  
  4.        android:layout_height="wrap_content"  
  5.        android:scaleType="centerCrop"  
  6.        android:adjustViewBounds="true"  
  7.        android:src="@drawable/load_default_img" />  

android:adjustViewBounds="true"必须与MaxHeight一起使用才能有效,所以,我要设置该ImageView的最大高度MaxHeight:
[html] view plain copy
  1. int screenWidth = getScreenWidth(this); // 获取屏幕宽度  
  2. ViewGroup.LayoutParams lp = testImage.getLayoutParams();  
  3. lp.width = screenWidth;  
  4. lp.height = LayoutParams.WRAP_CONTENT;  
  5. testImage.setLayoutParams(lp);  
  6.   
  7. testImage.setMaxWidth(screenWidth);  
  8. testImage.setMaxHeight(screenWidth * 5); //这里其实可以根据需求而定,我这里测试为最大宽度的5倍 
0 0
原创粉丝点击