Android ImageView display different pictures based on different status and level

Source: Internet
Author: User

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

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.