The application of statelistdrawable and gradientdrawable

Source: Internet
Author: User

In Android development, we often use custom drawable styles to set the shape style or selector selector in drawable, but sometimes if you need multiple drawable styles in one XML, And the style that is set inside only has the slight difference change, then the custom multiple drawable style will appear to be more bloated, make the drawable XML file too many, management trouble, so sometimes a word rabbit June think can create drawable in code.

/**
* Set Background Selector
*
* @param Presseddraw
* @param Normaldraw
* @return
*/
Private statelistdrawable Getselector (drawable normaldraw, drawable Presseddraw) {
Statelistdrawable statelistdrawable = new statelistdrawable ();
Statelistdrawable.addstate (New int[]{android. r.attr.state_selected}, Presseddraw);
Statelistdrawable.addstate (New int[]{}, Normaldraw);    return statelistdrawable; This is the creation of selector in code, the type created is statelistdrawable, and you can add states to selector by Addstate (), but be aware that when you add state, there is a sequence, Statelistdrawable executes the newly added state, and if it is not, it executes a large scale state before it is executed, and if it is not, it will result in a large state being executed directly, without executing the subsequent state. Additionally, in Add state, the "-" sign is added before the state to indicate that this state is false (for example:-android. r.attr.state_selected), otherwise true.

/**
* Set shape
*
* @param radius
* @param fillcolor
* @param width
* @param strokecolor
* @return
*/
Private gradientdrawable getdrawable (int radius, int fillcolor, int width, int strokecolor) {
Gradientdrawable gradientdrawable = new gradientdrawable ();
Gradientdrawable.setcornerradius (RADIUS);
Gradientdrawable.setcolor (FillColor);
Gradientdrawable.setstroke (width, strokecolor);
return gradientdrawable;
}

Creating gradientdrawable dynamically in your code allows you to implement the shape style in the drawable style, SetColor equals the fill color in shape, and Setcornerradius is setting the fillet radius in shape. If you want to set the radian of a single corner, you can set the radian of each corner with setcornerradii (), Setstroke () is the stroke, and the color of the width and edge of the stroke needs to be filled in.

Statelistdrawable and Gradientdrawable use

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.