I am a puzzled about using src
or background
for an ImageView
.
I know the former means the content of this ImageView
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.
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.
All views can take a background image.
The src
to an ImageView
has additional features:
adjustViewBounds
for setting bounds to match image dimensionsAnd more that you may find in the docs.
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
.
when you use android:src
, then image will display in its original size. No
automatic scaling, adjustments will happen.