Android custom Slide button, android slide

Source: Internet
Author: User

Android custom Slide button, android slide

When I first came into contact with a company project, I was asked to draw pages, which also involved custom views that I hated most. However, I had to do anything I hated. After searching Baidu resources, finally, a sliding control is written. Let's talk about the code.

 

 

 

Package com. etong. cpms. widget;

Import com. etong. cpms. activity. R;

Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Matrix;
Import android. graphics. Paint;
Import android. util. AttributeSet;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnTouchListener;

Public class WiperSwitch extends View implements OnTouchListener {
Private Bitmap bg_on, bg_off, slipper_btn;
Private String mOnText = "open ";
Private String mOffText = "disabled ";
/**
* X at press time and current x
*/
Private float downX, nowX;

/**
* Record whether the user is sliding
*/
Private boolean onSlip = false;

/**
* Current Status
*/
Private boolean nowStatus = false;

/**
* Listener Interface
*/
Private OnChangedListener listener;


Public WiperSwitch (Context context ){
Super (context );
Init ();
}

Public WiperSwitch (Context context, AttributeSet attrs ){
Super (context, attrs );
Init ();
}

Public void init (){
// Load image resources
Bg_on = BitmapFactory. decodeResource (getResources (), R. drawable. switch_bkg_switsource );
Bg_off = BitmapFactory. decodeResource (getResources (), R. drawable. switch_bkg_switsource );
Slipper_btn = BitmapFactory. decodeResource (getResources (), R. drawable. switch_btn_slip1 );
SetOnTouchListener (this );
}

Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Matrix matrix = new Matrix ();
Paint paint = new Paint ();
Float x = 0;

// Set the background, on, or off status based on nowX
If (nowX <(bg_on.getWidth ()/2 )){
Canvas. drawBitmap (bg_off, matrix, paint); // draws the background when the image is disabled.
Paint. setColor (Color. WHITE );
Paint. setFakeBoldText (true );
Paint. setTextSize (40 );
Canvas. drawText (mOffText, (bg_off.getWidth ()-slipper_btn.getWidth ()/2, (bg_off.getHeight () + 30)/2, paint );
} Else {
Canvas. drawBitmap (bg_on, matrix, paint); // draws the background when the image is opened.
Canvas. drawBitmap (bg_on, matrix, paint); // draws the background when the image is opened.
Paint. setColor (Color. WHITE );
Paint. setFakeBoldText (true );
Paint. setTextSize (40 );
Canvas. drawText (mOnText, (bg_on.getWidth ()-slipper_btn.getWidth ()/2, (bg_on.getHeight () + 30)/2, paint );
}

If (onSlip) {// whether it is in the slide status,
If (nowX> = bg_on.getWidth () // specifies whether to specify a range. This judgment is required because the slider cannot be redirected to an external header.
X = bg_on.getWidth ()-slipper_btn.getWidth ()/2; // minus the length of the slider 1/2
Else
X = nowX-slipper_btn.getWidth ()/2;
} Else {
If (nowStatus) {// set the x value of the slider based on the current status
X = bg_on.getWidth ()-slipper_btn.getWidth ();
} Else {
X = 0;
}
}

// Handle the slider slide exception, so the slider cannot be moved out of bounds
If (x <0 ){
X = 0;
}
Else if (x> bg_on.getWidth ()-slipper_btn.getWidth ()){
X = bg_on.getWidth ()-slipper_btn.getWidth ();
}

// Draw the slider
Canvas. drawBitmap (slipper_btn, x, 0, paint );
}

@ Override
Public boolean onTouch (View v, MotionEvent event ){
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN :{
If (event. getX ()> bg_off.getWidth () | event. getY ()> bg_off.getHeight ()){
Return false;
} Else {
OnSlip = true;
DownX = event. getX ();
NowX = downX;
}
Break;
}
Case MotionEvent. ACTION_MOVE :{
NowX = event. getX ();
Break;
}
Case MotionEvent. ACTION_UP :{
OnSlip = false;
If (event. getX ()> = (bg_on.getWidth ()/2 )){
NowStatus = true;
NowX = bg_on.getWidth ()-slipper_btn.getWidth ();
} Else {
NowStatus = false;
NowX = 0;
}

If (listener! = Null ){
Listener. OnChanged (WiperSwitch. this, nowStatus );
}
Break;
}
}
// Refresh the page
Invalidate ();
Return true;
}



/**
* Sets a listener for the WiperSwitch for external calls.
* @ Param listener
*/
Public void setOnChangedListener (OnChangedListener listener ){
This. listener = listener;
}

/**
* Set the initial status of the slide switch for external calls.
* @ Param checked
*/
Public void setChecked (boolean checked ){
If (checked ){
NowX = bg_off.getWidth ();
} Else {
NowX = 0;
}
NowStatus = checked;
}
/**
* Callback Interface
* @ Author len
*
*/
Public interface OnChangedListener {
Public void OnChanged (WiperSwitch wiperSwitch, boolean checkState );
}


}

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.