ImageView 常用属性的分析

最近在图片上犯了迷糊,在礼拜天抽出时间把属性一个一个

试了一遍,在这里和大家分享一下

ImageView的属性:

    android:src 用于显示图片

    android:maxWidth=""   最大宽度
    android:minHeight=""   最小高度
    android:maxHeight=""  最大高度
    android:minHeight=""  最小高度

 上面四个属性需要设置android:adjustViewBounds 属性设置为true 否则不起作用

为了验证看下面的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
   <ImageView
       android:id="@+id/iamgeView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
     
       android:src="@drawable/a" />
    
</LinearLayout>
ImageView 常用属性的分析_第1张图片

设置一下代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
   <ImageView
       android:id="@+id/iamgeView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        
       android:maxHeight="100dp"
       android:maxWidth="100dp"
       android:src="@drawable/a" />
    
</LinearLayout>
ImageView 常用属性的分析_第2张图片
效果没有改变,那添加上android:adjustViewBounds="true"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
   <ImageView
       android:id="@+id/iamgeView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:adjustViewBounds="true"
       android:maxHeight="100dp"
       android:maxWidth="100dp"
       android:src="@drawable/a" />
    
</LinearLayout>
在看效果图

ImageView 常用属性的分析_第3张图片
提醒 这个最大和最小是相对的,当你直接设置

android:layout_width=""
android:layout_height=""

之后图片的最大和和最小就失去作用了,而是直接取决于图片本身。



   android:adjustViewBounds :是否调整图片的边界来保持显示的长宽比

这个属性也是常用的,比如有的时候我们喜欢个图片的宽和高设置为具体的数字

其实可以这样设置的,有时候你可能感觉设置了不起作用那是因为最大或最小的

数值你没有设置的原因,其实这句话就是自己写这边博客的原因,



   <ImageView
       android:id="@+id/iamgeView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:adjustViewBounds="true"
       android:maxHeight="300dp"
       android:maxWidth="100dp"
       android:src="@drawable/a" />

ImageView 常用属性的分析_第4张图片

把宽和高自适应设置最大和最小,也能达到想要的效果, 有时候图片可能太大不能显示,自己看代码很久也找不到原因

不放设置下

android:adjustViewBounds="true"看看是不是图片太大的原因
 
 

 android:scaleType="":是控制图片如何resized/moved来匹对ImageViewsize  

属性有:

center  按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示

 centerCrop  按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)

 centerInside  将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽

 fitCenter  把图片按比例扩大/缩小到View的宽度,居中显示

 fitEnd   把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

 fitStart  把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

 fitXY  把图片不按比例扩大/缩小到View的大小显示






你可能感兴趣的:(imageview)