ImageView(图像视图)-src属性和background属性的区别:

来源:互联网 发布:电影php用什么打开 编辑:程序博客网 时间:2024/05/22 00:05

在API文档中我们发现ImageView有两个可以设置图片的属性,分别是:src和background

常识:

①background通常指的都是背景,而src指的是内容!!

②当使用src填入图片时,是按照图片大小直接填充,并不会进行拉伸

而使用background填入图片,则是会根据ImageView给定的宽度来进行拉伸

1)写代码验证区别:

写个简单的布局测试下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:id="@+id/LinearLayout1"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"      tools:context="com.jay.example.imageviewdemo.MainActivity" >        <ImageView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:background="@drawable/pen" />        <ImageView          android:layout_width="200dp"          android:layout_height="wrap_content"          android:background="@drawable/pen" />        <ImageView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:src="@drawable/pen" />        <ImageView          android:layout_width="200dp"          android:layout_height="wrap_content"          android:src="@drawable/pen" />    </LinearLayout> 

效果图如下:

结果分析:

宽高都是wrap_content那就一样,是原图大小,但是,当我们固定了宽或者高的话,差别就显而易见了,blackground完全填充了整个ImageView,而src依旧是那么大,而且他居中了哦,这就涉及到了ImageView的另一个属性scaleType了!另外还有一点,这里我们说了只设置width或者height哦!加入我们同时设置了width和height的话,blackground依旧填充,但是,src的大小可能发生改变哦!比如,我们测试下下面这段代码:

<ImageView          android:layout_width="100dp"          android:layout_height="50dp"          android:src="@drawable/pen" />

运行效果图:

阅读全文
0 0