Custom switchButton,

Source: Internet
Author: User

Custom switchButton,

This blog is about custom switchButton, but no animation effect is set.

I used GradientDrawable to draw the switchButton. Let's first look at the final effect:

Before clicking:

After clicking

 

Next let's take a look at how to implement

First, this class inherits RelativeLayout

SwitchButton extends RelativeLayout{
}

 

Members required for this class:

Private ImageView track; // the track where the slider is located. private ImageView slider; // the slider on the switchButton is private GradientDrawable trackDrawable; // used to draw the track private GradientDrawable sliderDrawable; // draw the slider private LayoutParams trackLy; // The Track Layout private LayoutParams sliderLy; // the slider LayoutPrivate boolean isCkeck = false; // whether to click

 

Constructor:

public SwitchButton(Context context) {        super(context);        init();    }    public SwitchButton(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }

 

Private void init () {setClickable (true); setLayoutParams (new LayoutParams (ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT); track = new ImageView (getContext (); slider = new ImageView (getContext (); trackLy = new LayoutParams (ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT); trackLy. addRule (CENTER_VERTICAL); sliderLy = new LayoutParams (ViewGroup. layoutParams. WRAP_CONTENT, ViewGroup. layoutParams. WRAP_CONTENT); sliderLy. addRule (CENTER_VERTICAL); track. setLayoutParams (trackLy); slider. setLayoutParams (sliderLy); trackDrawable = new GradientDrawable (); sliderDrawable = new GradientDrawable ();
// Track shape, color, size, edge, angle trackDrawable. setShape (GradientDrawable. RECTANGLE); trackDrawable. setColor (getResources (). getColor (R. color. white_FFFFFFFF); trackDrawable. setSize (getResources (). getDimensionPixelOffset (R. dimen. track_width), getResources (). getDimensionPixelOffset (R. dimen. track_height); trackDrawable. setStroke (getResources (). getDimensionPixelOffset (R. dimen. switch_button_stroke), getResources (). getColor (R. color. gray); trackDrawable. setCornerRadius (getResources (). getDimension (R. dimen. switch_button_cornerRadius ));
// Set the slider shape, color, size, edge, and angle sliderDrawable. setShape (GradientDrawable. OVAL); sliderDrawable. setColor (getResources (). getColor (R. color. white_FFFFFFFF); sliderDrawable. setSize (getResources (). getDimensionPixelOffset (R. dimen. slider_size), getResources (). getDimensionPixelOffset (R. dimen. slider_size); sliderDrawable. setStroke (getResources (). getDimensionPixelOffset (R. dimen. switch_button_stroke), getResources (). getColor (R. color. gray); // set to imageView track. setImageDrawable (trackDrawable); slider. setImageDrawable (sliderDrawable); addView (track); addView (slider); setOnClickListener (this );}

 

Set click events

@Override    public void onClick(View view) {        if (isCkeck)        {            isCkeck = false;        }else {            isCkeck = true;        }        setCheck(isCkeck);    }
Private void setCheck (boolean isCheck) {if (isCheck ){
// The orbit changes to blue trackDrawable. setColor (getResources (). getColor (R. color. blue ));
// Check the version here. If is the same as the code in else, but some code can be used in some versions if (Build. VERSION. SDK_INT> = 17) {sliderLy. addRule (ALIGN_PARENT_END );
// Remove the previous layout before setting a new layout. Otherwise, the stack effect is sliderLy. removeRule (ALIGN_PARENT_START);} else {sliderLy. addRule (ALIGN_PARENT_RIGHT); sliderLy. addRule (ALIGN_PARENT_LEFT, 0);} slider. setLayoutParams (sliderLy);} else {trackDrawable. setColor (getResources (). getColor (R. color. white_FFFFFFFF); if (Build. VERSION. SDK_INT> = 17) {sliderLy. removeRule (ALIGN_PARENT_END); sliderLy. addRule (ALIGN_PARENT_START);} else {sliderLy. addRule (ALIGN_PARENT_RIGHT, 0); sliderLy. addRule (ALIGN_PARENT_LEFT);} slider. setLayoutParams (sliderLy );}}

The above is all the code.

Please respect labor results, reprinted please indicate the source: http://www.cnblogs.com/tangZH/p/8277428.html

 

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.