View and viewimage

Source: Internet
Author: User

1. Set the background of the View class
Android: background -- setbackgroundresource (INT) -- A drawable to use as the background.

Setbackgrounddrawable (drawable d) --- set the background to a given drawable, or remove the background.
Corresponding retrieval method:
Getbackground () --- gets the background drawable



2. Set the background of the imageview class
Android: SRC -- setimageresource (INT) --- sets a drawable as the content of this imageview.

Setimagedrawable (drawable) ---- sets a drawable as the content of this imageview.
Corresponding retrieval method:
Getdrawable () ---- return the view's drawable, or null if no drawable has been assigned.

Q: What is the difference between SRC and background in imageview?
When using SRC, the source image is displayed, and the image size should not be changed; when using background, the image is enlarged or reduced according to the size of the component.

3. Different images are displayed in the same imageview.
Sometimes, to display different images in the same imageview, we often use:
If (condition 1 ){
Image. setbackground (R. Id. XXX1 );
} Else if (condition 2 ){
Image. setbackground (R. Id. xxx2 );
}
You can use another simple method to implement the same function. First, create an XML file under Res/drawable with the following content:
<Level-list xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: maxlevel = "4" Android: drawable = "@ drawable/stat_sys_battery_0"/>
<Item Android: maxlevel = "14" Android: drawable = "@ drawable/stat_sys_battery_10"/>
<Item Android: maxlevel = "29" Android: drawable = "@ drawable/stat_sys_battery_20"/>
<Item Android: maxlevel = "49" Android: drawable = "@ drawable/stat_sys_battery_40"/>
<Item Android: maxlevel = "69" Android: drawable = "@ drawable/stat_sys_battery_60"/>
<Item Android: maxlevel = "89" Android: drawable = "@ drawable/stat_sys_battery_80"/>
<Item Android: maxlevel = "100" Android: drawable = "@ drawable/stat_sys_battery_100"/>
</Level-List>
Then, in layout, set the SRC of the Image view to a created XML file. When converting the image in the program, you only need to use:
Imageview. getdrawable (). setlevel (50 );
Android automatically selects the corresponding image based on the level value. This method is used to display different pictures of the remaining power displayed on the mobile phone.

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.