Android--Selector&&statelistdrawable

Source: Internet
Author: User

selector
<?XML version= "1.0" encoding= "Utf-8"?>     <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">     <!--when touched and the current window is in interactive state -      <Itemandroid:state_pressed= "true"android:state_window_focused= "true"android:drawable= "@drawable/pic1" />    <!--touch and do not get focus state -      <Itemandroid:state_pressed= "true"android:state_focused= "false"android:drawable= "@drawable/pic2" />      <!--picture Background When selected -      <Itemandroid:state_selected= "true"android:drawable= "@drawable/pic3" />       <!--picture background when getting focus -      <Itemandroid:state_focused= "true"android:drawable= "@drawable/pic4" />      <!--The window does not have a background image at the time of interaction -      <Itemandroid:drawable= "@drawable/pic5" />   </selector>
statelistdrawable

This class defines the corresponding picture resources under different state values, that is, we can use this class to save a variety of state values, a variety of picture resources.

 Public void addstate (int[] stateset, drawable drawable)

To set the Drawable picture resource for a particular state collection, referring to the previous selector XML file, we use the code to construct the same Statelistdrawable class object

//Initialize an empty objectStatelistdrawable stalistdrawable =Newstatelistdrawable (); //gets the property value corresponding to the properties of the Android frame attrintpressed =Android.  r.attr.state_pressed; intwindow_focused =Android.  r.attr.state_window_focused; intfocused =Android.  r.attr.state_focused; intSelected =Android.    r.attr.state_selected; Stalistdrawable.addstate (New int[]{pressed, window_focused}, Getresources (). getdrawable (R.drawable.pic1)); Stalistdrawable.addstate (New int[]{pressed,-focused}, Getresources (). getdrawable (R.DRAWABLE.PIC2); Stalistdrawable.addstate (New int[]{selected}, Getresources (). getdrawable (R.DRAWABLE.PIC3); Stalistdrawable.addstate (New int[]{focused}, Getresources (). getdrawable (R.DRAWABLE.PIC4); //There is no state when the picture is displayed, we set it to my empty collectionStalistdrawable.addstate (New int[]{}, Getresources (). getdrawable (R.DRAWABLE.PIC5);

The "-" minus sign above indicates that the corresponding property value is False

 Public boolean isstateful ()

Indicates that the state has changed, and whether the corresponding drawable image will change. (In the Statelistdrawable class, the method returns True, and when the state changes, our picture changes.) )

Status

Android:state_selected Selected
android:state_focused Get Focus
android:state_pressed Click
android:state_enabled setting whether to respond to an event, refers to all events

the difference between selected and focused
    1. Selected is different from the focus state, typically under a Adapterview class such as a ListView or GridView, a view is in selected state, and the view that gets that state is highlighted.
    2. A window can have only one view of focus (focus), and a window may have multiple views in the "selected" state.
    • Focused state is usually caused by key operation;
    • The pressed state is caused by a touch message;
    • Selected is completely controlled by the application's active invocation of setselected ().
Analysis

When any state value of view changes, the Refreshdrawablelist () method is called to update the corresponding background drawable object.

//The main function is to replace the corresponding background drawable object according to the current status value .     Public voidrefreshdrawablestate () {mprivateflags|=Drawable_state_dirty; //all functions are done in this function.drawablestatechanged (); //...      }      //gets the current state attribute---Integral collection, and invokes the SetState method of the Drawable class to get the resource.     protected voiddrawablestatechanged () {//the Drawable object that corresponds to the view, usually corresponding to the Statelistdrawable class objectdrawable d =mbgdrawable; if(d! =NULL&& d.isstateful ()) {//They're usually set up.//getdrawablestate () method main function: It will be converted to an integral collection based on the current view's State property value//setState () method main function: According to the current acquired state, update the corresponding state of the Drawable object. d.setstate (Getdrawablestate ()); }      }       Public Final int[] Getdrawablestate () {if((Mdrawablestate! =NULL) && ((Mprivateflags & drawable_state_dirty) = = 0)) {              returnmdrawablestate; } Else {              //converts it to an integral collection based on the current view's State property value, and returnsMdrawablestate = oncreatedrawablestate (0); Mprivateflags&= ~Drawable_state_dirty; returnmdrawablestate; }      }

SetState ()

// if the state value changes, the Onstatechange () method is recalled.    Public boolean setState (finalint[] stateset) {      if (!  Arrays.equals (Mstateset, Stateset)) {          = stateset;           return Onstatechange (Stateset);      }       return false ;  }

Determine if the state value has changed and, if so, call the Onstatechange () method to process it further.

 //  The state value has changed and we need to find out the first Drawable object that matches the current state.  protected  boolean  Onstatechange (int  [] stateset) { //  int  idx = Mstateliststate.indexofstateset (Stateset); ...       //     if   (selectdrawable (idx)) { return  true  ; }      ...  }

Based on the new status value, from the Statelistdrawable instance object, find the first index that exactly matches the new state value, and then call the Selectdrawable () method to get the current Drawable object under the index.

 Public BooleanSelectdrawable (intidx) {if(idx >= 0 && idx <Mdrawablecontainerstate.mnumchildren) {//gets the Drawable object for the corresponding index positiondrawable d =Mdrawablecontainerstate.mdrawables[idx]; . .. mcurrdrawable= D;//Mcurrdrawable Even if the current Drawable objectMcurindex =idx; ...      } Else {         ...      } //request the view to refresh itself, this method we will explain later. invalidateself (); return true; }
drawable.callback                                                                        
 public  static  interface   Callback { //  If the state of the Drawable object changes, the view is requested to redraw,  //   So we can "draw out" the background drawable object that corresponds to the view.  public  void   Invalidatedrawable (drawable who);  public  void  scheduledrawable (       Drawable who, Runnable what, long   public  void   Unscheduledrawable (drawable who, Runnable); }

The Android Framework view class inherits the interface and implements the default handling of these three functions, where the invalidatedrawable() method is as follows:

 Public classViewImplementsDrawable.callback, Keyevent.callback, Accessibilityeventsource { ...//invalidates the specified drawable. //the default implementation, redrawing the view itself     Public voidinvalidatedrawable (drawable drawable) {if(Verifydrawable (drawable)) {//is the same drawable object, usually true            FinalRect dirty =drawable.getbounds (); Final intSCROLLX =Mscrollx; Final intscrolly =mscrolly; //re-request to draw the view, called the Draw () method of the view again ...Invalidate (Dirty.left + scrollx, Dirty.top +scrolly, Dirty.right+ SCROLLX, Dirty.bottom +scrolly); }      }      ...  }

Therefore, our Drawable class object must have View set to the callback object, otherwise the corresponding background graph will not be displayed even if the state is changed.

drawable d  ;                // Picture Resources                         D.setcallback (View v);  // the background resource for view V is a D object
I'm the dividing line of the king of the land Tiger.

Reference: http://blog.csdn.net/qinjuning/article/details/7474827

Android--Selector&&statelistdrawable

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.