ImageView的講解一、src和background的區別background我們通常理解是背景,而src是內容,當使用src填入圖片的時候是以圖片的大小直接填充,並不會進行拉伸,而background填入圖片會根據指定的大小對圖片進行縮放拉伸。 當我們設置寬和高都...
ImageView的講解
一、src和background的區別
background我們通常理解是背景,而src是內容,當使用src填入圖片的時候是以圖片的大小直接填充,並不會進行拉伸,而background填入圖片會根據指定的大小對圖片進行縮放拉伸。
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ffffff"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher"/> <ImageView android:layout_width="200dp" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <ImageView android:layout_width="200dp" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> </LinearLayout>
當我們設置寬和高都是wrap_content的時候background和src都是原圖片的大小,但是當我們將layout_height設置了高度的時候差別就顯然可見,我們會發現src屬性的時候圖片的寬度沒有那麼寬,這時圖片就會自動居中,同時如果寬和高設置都不能滿足圖片的要求的時候這時就是涉及到了圖片縮放了ScaleType屬性。同時如果兩者結合使用還有其他的一些效果。
二.設置圖片的透明度
前面所過兩個屬性的區別,現在我們聊一下如何設置一個圖片的透明度,很簡單,我們只需要在Java代碼調用setAlpha方法就可以了
但是我們需要註意的是,這個方法只能對src屬性的控制項有作用,參數是float類型的。
三、adjustViewBounds設置縮放是否保持原圖的長寬比例
這個屬性單獨的使用沒有什麼作用,我們需要同過maxHeight和maxWidth結合使用才能起作用
maxHeight:是imageView的最大高度
maxeWidth:是imageView的最大寬度
<?xml version="1.0" encoding="utf-8"?>![]()