Recently, I was working on a WiFi project to show the signal strength of a hotspot with a picture and whether a password is required.
It's like this:
A hotspot has four levels of signal strength, requires a password, and does not require two states.
To implement this function, you can judge its state, strength, and then setimageresource in the code. But this is more cumbersome.
After looking at the source code of the Android Settings WiFi module, we found that there is a more convenient way.
In Java code, find the ImageView of signal, set its rank, state:
ImageView signal = ( ImageView) convertview.findviewbyid (r.id.signal); if (Accesspoint.getrssi () == integer.max_value) { signal.setimagedrawable (NULL); } else { signal.setimagelevel ( Accesspoint.getlevel ()); signal.setimagestate ((accesspoint.getsecurity () != accesspoint.security_none) ? accesspOint. State_secured : accesspoint.state_none, true); }
The interface put the ImageView position, android:src= "@drawable/wifi_signal_light"
<imageview android:id= "@+id/signal" android:layout_width= "wrap_content" android:layout_height= "Wra P_content "android:layout_alignparentright=" true "android:layout_alignparenttop=" true "Android:conten tdescription= "@null" android:src= "@drawable/wifi_signal_light"/>
Wifi_signal_light.xml
<selector xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:settings= "http// Schemas.android.com/apk/res/com.mango.gnet "> <item settings:state_encrypted=" true "android:drawable=" @ Drawable/wifi_signal_lock_light "/> <item settings:state_encrypted=" false "android:drawable=" @drawable/wifi_ Signal_open_light "/></selector>
And finally, Wifi_signal_lock_light and Wifi_signal_open_light. Two file (s)
<level-list xmlns:android= "http:// Schemas.android.com/apk/res/android "> <item android:maxlevel=" 0 " android:drawable= "@drawable/ic_wifi_lock_signal_1_light" /> <item Android:maxlevel= "1" android:drawable= "@drawable/ic_wifi_lock_signal_2_light" /> <item android:maxlevel= "2" android:drawable= "@drawable/ic_wifi_lock_signal_3_light" /> <item android:maxlevel= "3" android:drawable= "@drawable/ic_wifi _lock_signal_4_light "&NBSP;/></LEVEL-LIST>
<level-list xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:maxlevel= "0" android:drawable= "@drawable/ic_wifi_signal_1_light"/> <item android:maxlevel= "1" android:drawable= "@ Drawable/ic_wifi_signal_2_light "/> <item android:maxlevel=" 2 "android:drawable=" @drawable/ic_wifi_signal_3_ Light "/> <item android:maxlevel=" 3 "android:drawable=" @drawable/ic_wifi_signal_4_light "/></level-list >
The overall idea is
Signal.setimagestate ()
will decide which file to use according to the set state.
Setimagelevel ()
Will decide to use the level-list image according to the set array.
Android ImageView display different pictures based on different status and level