zoukankan      html  css  js  c++  java
  • ImageView的src和background的区别

    参考资料:

    http://blog.csdn.net/dalleny/article/details/14048375

    http://www.android100.org/html/201508/27/177067.html

    区别如下:

    1、  background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸,如果想要对src进行拉伸的话,使用android:scaleType

    2、  src是图片内容(前景),bg是背景,可以同时使用。

    3、  此外:scaleType只对src起作用;bg可设置透明度

    4、  Src对应的Java代码是:imgView.setImageResource(R.drawable.*)

    Background对应的Java代码是:imgView.setBackgroundResource(R.drawable.*);

    5、  scaleType说明

    CENTER /center 在视图中心显示图片,并且不缩放图片

    CENTER_CROP / centerCrop 按比例缩放图片,使得图片长 (宽)的大于等于视图的相应维度

    CENTER_INSIDE / centerInside 按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维度

    FIT_CENTER / fitCenter 按比例缩放图片到视图的最小边,居中显示

    FIT_END / fitEnd 按比例缩放图片到视图的最小边,显示在视图的下部分位置

    FIT_START / fitStart 把图片按比例扩大/缩小到视图的最小边,显示在视图的上部分位置

    FIT_XY / fitXY 把图片不按比例缩放到视图的大小显示

    MATRIX / matrix 用矩阵来绘制

    区别效果图:

     

    代码:

      1 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      2     xmlns:tools="http://schemas.android.com/tools"
      3     android:layout_width="match_parent"
      4     android:layout_height="match_parent"
      5     android:background="#00000000"
      6     android:scrollbars="vertical"
      7     tools:context="com.example.imageviewsrcandbg.MainActivity" >
      8 
      9     <LinearLayout
     10         android:layout_width="match_parent"
     11         android:layout_height="wrap_content"
     12         android:orientation="vertical" >
     13 
     14         <TextView
     15             android:layout_width="wrap_content"
     16             android:layout_height="wrap_content"
     17             android:text="1、ImageViewd的src和background的区别:" />
     18 
     19         <TextView
     20             android:layout_width="wrap_content"
     21             android:layout_height="wrap_content"
     22             android:text="(1)background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸。如果想要对src进行拉伸的话,使用android:scaleType" />
     23         <!-- =============================================================== -->
     24         <TextView
     25             android:layout_width="wrap_content"
     26             android:layout_height="wrap_content"
     27             android:text="(1.1)固定的宽高(100*60)ImageView控件background,图片是66" />
     28 
     29         <!-- 固定的宽高ImageView控件 -->
     30 
     31         <ImageView
     32             android:layout_width="100dp"
     33             android:layout_height="60dp"
     34             android:background="@drawable/load_circle" />
     35         <!-- =============================================================== -->
     36         <TextView
     37             android:layout_width="wrap_content"
     38             android:layout_height="wrap_content"
     39             android:text="(1.2)固定的宽高(100*60)ImageView控件src,图片是66" />
     40 
     41         <!-- 固定的宽高ImageView控件 -->
     42 
     43         <ImageView
     44             android:layout_width="100dp"
     45             android:layout_height="60dp"
     46             android:src="@drawable/load_circle" />
     47         <!-- =============================================================== -->
     48         <!-- src和background的结合使用 -->
     49 
     50         <TextView
     51             android:layout_width="wrap_content"
     52             android:layout_height="wrap_content"
     53             android:text="(2)固定的宽高(100*60)ImageView控件src是图片内容(前景),bg是背景,可以同时使用。" />
     54         <!-- 固定的宽高ImageView控件 -->
     55 
     56         <ImageView
     57             android:layout_width="100dp"
     58             android:layout_height="60dp"
     59             android:background="@drawable/load_circle"
     60             android:src="@drawable/load_logo" />
     61         <!-- =============================================================== -->        
     62          <!-- src的拉伸:scaleType -->
     63 
     64         <TextView
     65             android:layout_width="wrap_content"
     66             android:layout_height="wrap_content"
     67             android:text="(3)scaleType只对src起作用,对src进行拉伸(固定的宽高(100*60)ImageView控件)" />
     68         
     69         <TextView
     70             android:layout_width="wrap_content"
     71             android:layout_height="wrap_content"
     72             android:text="(3.1)CENTER /center 在视图中心显示图片,并且不缩放图片" />
     73         
     74         <!-- 固定的宽高ImageView控件 -->
     75         <ImageView
     76             android:layout_width="100dp"
     77             android:layout_height="60dp"
     78             android:src="@drawable/load_circle"
     79             android:scaleType="center"
     80              />
     81         <!-- =============================================================== -->        
     82         <TextView
     83             android:layout_width="wrap_content"
     84             android:layout_height="wrap_content"
     85             android:text="(3.2)CENTER_CROP / centerCrop 按比例缩放图片,使得图片长 (宽)的大于等于视图的相应维度" />
     86         
     87         <!-- 固定的宽高ImageView控件 -->
     88         <ImageView
     89             android:layout_width="100dp"
     90             android:layout_height="60dp"
     91             android:src="@drawable/load_circle"
     92             android:scaleType="centerCrop"
     93              />
     94         <!-- =============================================================== -->        
     95         <TextView
     96             android:layout_width="wrap_content"
     97             android:layout_height="wrap_content"
     98             android:text="(3.3)CENTER_INSIDE / centerInside 按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维度" />
     99         
    100         <!-- 固定的宽高ImageView控件 -->
    101         <ImageView
    102             android:layout_width="100dp"
    103             android:layout_height="60dp"
    104             android:src="@drawable/load_circle"
    105             android:scaleType="centerInside"
    106              />
    107         <!-- =============================================================== -->        
    108         <TextView
    109             android:layout_width="wrap_content"
    110             android:layout_height="wrap_content"
    111             android:text="(3.4)FIT_CENTER / fitCenter 按比例缩放图片到视图的最小边,居中显示" />
    112         
    113         <!-- 固定的宽高ImageView控件 -->
    114         <ImageView
    115             android:layout_width="100dp"
    116             android:layout_height="60dp"
    117             android:src="@drawable/load_circle"
    118             android:scaleType="fitCenter"
    119              />
    120         <!-- =============================================================== -->        
    121         <TextView
    122             android:layout_width="wrap_content"
    123             android:layout_height="wrap_content"
    124             android:text="(3.5)FIT_END / fitEnd 按比例缩放图片到视图的最小边,显示在视图的下部分位置——短边的下面或者右面" />
    125         
    126         <!-- 固定的宽高ImageView控件 -->
    127         <ImageView
    128             android:layout_width="100dp"
    129             android:layout_height="60dp"
    130             android:src="@drawable/load_circle"
    131             android:scaleType="fitEnd"
    132              />
    133         <!-- =============================================================== -->        
    134         <TextView
    135             android:layout_width="wrap_content"
    136             android:layout_height="wrap_content"
    137             android:text="(3.6)FIT_START / fitStart 把图片按比例扩大/缩小到视图的最小边,显示在视图的上部分位置——短边的上面或者左面" />
    138         
    139         <!-- 固定的宽高ImageView控件 -->
    140         <ImageView
    141             android:layout_width="100dp"
    142             android:layout_height="60dp"
    143             android:src="@drawable/load_circle"
    144             android:scaleType="fitStart"
    145              />
    146         <!-- =============================================================== -->        
    147         <TextView
    148             android:layout_width="wrap_content"
    149             android:layout_height="wrap_content"
    150             android:text="(3.7)FIT_XY / fitXY 把图片不按比例缩放到视图的大小显示——类似background" />
    151         
    152         <!-- 固定的宽高ImageView控件 -->
    153         <ImageView
    154             android:layout_width="100dp"
    155             android:layout_height="60dp"
    156             android:src="@drawable/load_circle"
    157             android:scaleType="fitXY"
    158              />
    159         <!-- =============================================================== -->        
    160         <TextView
    161             android:layout_width="wrap_content"
    162             android:layout_height="wrap_content"
    163             android:text="(3.8)MATRIX / matrix 用矩阵来绘制——左上角开始绘制" />
    164         
    165         <!-- 固定的宽高ImageView控件 -->
    166         <ImageView
    167             android:layout_width="100dp"
    168             android:layout_height="60dp"
    169             android:src="@drawable/load_circle"
    170             android:scaleType="matrix"
    171              />
    172     </LinearLayout>
    173 
    174 </ScrollView>
    activity_mian.xml

     素材(采用的是京东的loading图片):

  • 相关阅读:
    剑指offer:面试题25、二叉树中和为某值的路径
    剑指offer:面试题24、二叉搜索树的后续遍历序列
    剑指offer:面试题23、从上往下打印二叉树
    剑指offer:面试题22、栈的压入、弹出序列
    剑指offer:面试题21、包含min函数的栈
    剑指offer:面试题20、顺时针打印矩阵
    剑指offer:面试题19、二叉树的镜像
    剑指offer:面试题18、树的子结构
    剑指offer:面试题17、合并两个排序的链表
    剑指offer:面试题16、反转链表
  • 原文地址:https://www.cnblogs.com/whycxb/p/5031895.html
Copyright © 2011-2022 走看看