Android Selector principle

Source: Internet
Author: User

Android Selector is more familiar to Android developers, as long as you define a selector XML file in a drawable directory, Using this XML file in a layout file, or setbackgrounddrawable it in code, enables the effect of different states, such as a control's press or focus.

Then why do you realize this function after setbackgrounddrawable?

First, to understand a drawable class, drawable is an abstract, ready-to-draw picture class that can create a picture from a local path, or it can be created from a well-defined XML. They correspond to the Createfrompath and createfromxml functions of drawable, where Createfrompath is to create a bitmap object from the path and convert it to bitmapdrawable, While Createfromxml is a label defined from XML, such as selector, creates a Statelistdrawable object, and shape creates a Gradientdrawable object. Color, create colordrawable ... And Bitmapdrawable, statelistdrawable, gradientdrawable are derived from the Drawable class. Where the Statelistdrawable class is the drawable that implements the style defined in selector.

Next we see how drawable is associated with the view.

The Drawable class has a variable mstateset that maintains a different state of a control, When View.setbackgrounddrawable, Drawable's isstateful function is called to determine if there is a different state, Statelistdrawable returns True, If it is stateful, the state of the view will be assigned to drawable that is D.setstate (Getdrawablestate ());

if (d.isstateful ()) {

D.setstate (Getdrawablestate ());

}

The incoming drawable is also used as the background drawable. When the control receives a touch event, the Refreshdrawablestate update control state is called, and the drawable state of the background is also updated

protected void drawablestatechanged () {

drawable d = mbgdrawable;

if (d! = null && d.isstateful ()) {

D.setstate (Getdrawablestate ());

}

}

This callback function is then called Invalidatedrawable to refresh the interface, and the draw function is called to implement the drawing.

Again we see the implementation of the selector function drawable that statelistdrawable is how to implement the selector function.

As we have seen above, the SetState function of drawable is called when the view state changes. This is how setstate is implemented in drawable.

Public boolean setState (final int[] stateset) {

if (! Arrays.equals (Mstateset, Stateset)) {

Mstateset = Stateset;

Return Onstatechange (Stateset);

}

return false;

}

It calls Onstatechage when it changes state to notify the state that it has changed. And Statelistdrawable is a subclass of inherited drawable, and it onstatechage functions.

Protected Boolean Onstatechange (int[] stateset) {

int idx = Mstateliststate.indexofstateset (stateset);

if (DEBUG) android.util.log.i (TAG, "onstatechange" + This + "states"

+ arrays.tostring (stateset) + "found" + idx);

if (IDX < 0) {

IDX = Mstateliststate.indexofstateset (Stateset.wild_card);

}

if (selectdrawable (idx)) {

return true;

}

Return Super.onstatechange (Stateset);

}

From the above implementation can see it in changing the state of the time will call selectdrawable to select a current state of the drawable, this is the key to the implementation. Statelistdrawable inherited Drawablecontainer and Drawablecontainer inherited Drawable,stateliststate is the inner class of statelistdrawable, It is the implementation of the drawable that holds the different states defined in selector, which provides the Addstateset function to increase the corresponding drawable object in a state and save it in the mstatesets variable. The Indexofstateset function is to find the corresponding drawable in a certain state. Selectdrawable is the Drawablecontainer class that finds the corresponding drawable according to the index of the incoming state as drawable in the current state.

OK, now I can finally understand why selector is implementing different styles. View uses drawable to achieve the background map, selector corresponding to statelistdrawable, when the view state changes, will change the state of drawable, Statelistdrawable, when changing the state, selects the corresponding drawable according to the current state, so that the draw function of drawable is called when the view is drawn statelistdrawable Draw is the drawable that corresponds to the current state.

Android Selector principle

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.