ImageView的adjustViewBounds屬性再解讀

------ adjustViewBounds屬性要求api level 不小於18

 If the application targets API level 17 or lower, adjustViewBounds will allow the drawable to shrink the view bounds,
  but not grow to fill available measured space in all cases. 
  This is for compatibility with legacy MeasureSpec and RelativeLayout behavior.

------- 代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gaos.viewanimation.MainActivity">


    <ImageView
        android:id="@+id/imgview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/superman" />

</RelativeLayout>



----------- 當我不限制ImageView的高度時

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gaos.viewanimation.MainActivity">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/imgview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/superman" />
    </ScrollView>
</RelativeLayout>



① 當不限制ImageView的高度,在原圖寬高比爲700:1120的情況下,圖片寬度匹配了ImageView的最大寬度,高度不限制,所以adjustViewBounds屬性實現了圖片的寬度match_parent




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章