Android Imgview Properties

Source: Internet
Author: User

ImageView is a control that is used to display pictures on the interface.

Attribute 1, set picture for ImageView

① android:src= "@drawable/img1";

SRC sets the picture, the default picture, and so on scale, to the most suitable size display.

② android:background= "@drawable/img1"

Background is a common component property that allows you to set the background color of a component, as well as a picture.

"Hint" ① the background with a picture, then the picture will fit the size of the component.

② But if the control is wrap_content wide, the effect is the same as SRC.

③ if the SRC and background properties are set at the same time, the SRC settings picture will be above, and the background settings picture will be above. The SRC image does not necessarily cover the image below, depending on the src indent mode.

The ④ resource file name consists of lowercase letters, numbers, and underscores. (Note: cannot use uppercase letters)

③ Case

"Prepare" the corresponding picture resources can be placed in the Res/drawable folder, which is two pictures without any zoom effect

Code

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Android.support.constraint.ConstraintLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:app= "Http://schemas.android.com/apk/res-auto"4 Xmlns:tools= "Http://schemas.android.com/tools"5 Android:layout_width= "Match_parent"6 Android:layout_height= "Match_parent"7 Tools:context=". Mainactivity "8 Android:background= "#ccc">9     Ten     <ImageView One         Android:id= "@+id/iv" A Android:layout_width= "200DP" - Android:layout_height= "300DP" - Android:background= "@drawable/img1" /> the  -     <ImageView -         Android:id= "@+id/iv2" - Android:layout_width= "300DP" + Android:layout_height= "200DP" - Android:layout_marginbottom= "8DP" + Android:layout_marginend= "8DP" A Android:background= "@drawable/img2" at App:layout_constraintbottom_tobottomof= "Parent" - app:layout_constraintend_toendof= "Parent" /> -  - </Android.support.constraint.ConstraintLayout>

Effect

Prompted

"Hint" ① here in order to better illustrate the difference between background and SRC, we will imageview set the specific width.

② other properties such as margin and constraintend are only used to adjust the position. Depends on your parent container.

③ left is the effect of background, on the right is the effect of SRC, did not add a picture as the background, easy to observe the position and size of ImageView.

2. ScaleType attribute
Android:scaletype= "Fitxy"

"Hint" ScaleType property to use with the SRC attribute, no effect on the picture set by background

Code

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Android.support.constraint.ConstraintLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:app= "Http://schemas.android.com/apk/res-auto"4 Xmlns:tools= "Http://schemas.android.com/tools"5 Android:layout_width= "Match_parent"6 Android:layout_height= "Match_parent"7 Tools:context=". Mainactivity "8 Android:background= "#ccc">9 Ten     <ImageView One         Android:id= "@+id/iv" A Android:layout_width= "360DP" - Android:layout_height= "500DP" - android:src= "@drawable/img1" the Android:background= "#f00" - Android:scaletype= "Fitcenter"/> -  - </Android.support.constraint.ConstraintLayout>

"Property Value"

The following only modifies the ScaleType property values

①Fixcenter: This is the default property value for the picture, which means that the control is populated and does not deform the picture.

②Fixxy: Represents a picture fill control that allows the picture to be stretched to fit according to the size of the ImageView. and background the same effect.

③centercrop: To fill the entire ImageView, the center of the ImageView is aligned to the center of the ImageView, and so on to enlarge the original image until it fills the ImageView (the width of the imageview is filled), The original image is cropped out of the section.

Effect

④Center: keeps the original size, displayed in the center of the ImageView. When the original size is smaller than the ImageView size, the next part will be blank, as shown on the left. Conversely, the original image will be cut, such as the right figure (here the size of the ImageView is changed to a smaller than the original value)

Effect

⑤Matrix: Does not change the size of the original, starting from the upper left corner of the ImageView to draw the original. The original part will be cropped.

"Hint" with the matrix to zoom in and zoom out of the effect is that the ImageView ScaleType must be set to the matrix ImageView

⑥fitend: The original image is proportionally scaled to the width of the ImageView, shown in the lower position. Left image

⑦Fitstart: The original image is proportionally scaled to the width of the ImageView, displayed in the upper position. Right image

⑧centerinside: For the purpose of full display of the original image, the content of the picture is fully centered, by proportionally reducing the width of the original is equal to or less than the width of the imageview height. If the original image is less than the width of the ImageView, the original artwork is not processed and centered on the ImageView (as shown on the left). Conversely, the effect is the same as Fixcenter. The picture is scaled on the short side as a benchmark, and is displayed in the middle of the ImageView (pictured right).

3, adjust the boundary to adapt to the picture
Android:adjustviewbounds= "True/false"

Indicates whether the picture can be adapted by adjusting the bounds (used with maxWidth or maxheight ). This property is typically used in conjunction with the MaxHeight and MaxWidth properties. Maximum width and height.

"Hint" ① if the set layout_width and layout_height are fixed values, then setting adjustviewbounds is not effective, ImageView will be set to the width of the fixed value.

② if the set of Layout_width and Layout_height are wrap_content, then setting adjustviewbounds is meaningless, because ImageView will always have the same aspect ratio as the picture (but not the same width-height value , it usually enlarges some)

Android Imgview Properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.