What is the difference between src and background of ImageView

前端 未结 4 1705
[愿得一人]
[愿得一人] 2020-11-28 06:03

I am a puzzled about using src or background for an ImageView.

I know the former means the content of this ImageView

相关标签:
4条回答
  • 2020-11-28 06:05

    If you set an image to be the background of your ImageView, then the image will scale to whatever size the ImageView is. Other than that, src is a foreground image and background is a background image. Pretty much as it implies.

    0 讨论(0)
  • 2020-11-28 06:12

    The difference between XML attribute src and background in ImageView:

    The background will stretch according to the length given by the ImageView component, and SRC will hold the size of the original image without stretching. SRC is the picture content (foreground), BG is the background, can be used at the same time.

    In addition: ScaleType only works on SRC; BG can set transparency, for example, in ImageButton, you can use Android:scaletype to control how the image is scaled, sample code as follows:

     <imageview android:id= "@+id/img" 
      android:src= "@drawable/logo
      " android:scaletype= "Centerinside" Android:layout_width= "60dip"
      android:layout_height= "60dip"
      android:layout_centervertical= "true"/>
    

    Feel free to ask doubt if you get any.

    0 讨论(0)
  • 2020-11-28 06:13

    All views can take a background image.

    The src to an ImageView has additional features:

    • different scaling types
    • adjustViewBounds for setting bounds to match image dimensions
    • some transformations such as alpha-setting

    And more that you may find in the docs.

    0 讨论(0)
  • 2020-11-28 06:26
    1. when you use android:background, image will be set to fit on ImageView area(i.e according to width and height of ImageView). It doesn't matter if the image is smaller or larger than ImageView.

    2. when you use android:src, then image will display in its original size. No automatic scaling, adjustments will happen.

    0 讨论(0)
提交回复
热议问题