[Android] The difference and understanding between Drawable and src

Explain in detail

In Android, the src attribute and background attribute of ImageView are used to set different types of image content. Here is their detailed explanation:

src 属性: This attribute is used to set the image content displayed in ImageView. It can accept a reference to an image resource, which can be an image file, a vector graphic, or the URL of a network image.
Sample code:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/my_image" />

background attribute: This attribute is used to set the background image or background color of ImageView. It can accept a reference to an image resource or a color value, which can be a picture file or a color value.
Sample code:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_background" />

The differences and usage are summarized as follows:

  • The src attribute is used to set the foreground image content of ImageView, that is, the image is displayed above the ImageView.
  • The background property is used to set the background image or background color of ImageView, that is, the image is displayed in the background of ImageView.

When the src attribute is set to an image resource, the image will be displayed according to its original dimensions and proportions. You can use the scaleType property to adjust how the image is scaled.

When the background property is set to an image resource, the image will be stretched to fill the entire ImageView's background area.
When using the src attribute, you can use the android:tint attribute to apply a color filter to the image to achieve the coloring effect of the image.
When using the background attribute, you can use the android:backgroundTint attribute to apply a color filter to the background image to achieve the coloring effect of the background image.
Please note that the src and background attributes can be used in the same ImageView at the same time to display image content in the foreground and background of the image at the same time.

Guess you like

Origin blog.csdn.net/weixin_44002043/article/details/133860388