Different images are displayed in the same ImageView in Android.

Source: Internet
Author: User

Sometimes, to display different images in the same ImageView, we often use:
View plaincopy to clipboardprint?
If (condition 1 ){
Image. setBackground (R. id. xxx1 );
} Else if (condition 2 ){
Image. setBackground (R. id. xxx2 );
}
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:
View plaincopy to clipboardprint?
<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>
<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> in layout, set the src of the image view to an xml file that has been created. When converting images in a 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.

Author: "Li Xin's Crazy Android life ..."

Related Article

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.