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 ..."