Android Custom Swipe button

Source: Internet
Author: User

First Contact company Project let me draw the page, but also related to my most hated custom view but no way, hate also must do ah, through the search for resources on Baidu, finally wrote a sliding control. Nonsense not much to say, on 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 = "Off";
/**
* Press the X and the current X
*/
Private float downx, nowx;

/**
* Record whether the user is sliding
*/
Private Boolean onslip = false;

/**
* Current status
*/
Private Boolean nowstatus = false;

/**
* Monitoring Interface
*/
Private Onchangedlistener listener;


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

Public Wiperswitch (context context, AttributeSet Attrs) {
Super (context, attrs);
Init ();
}

public void init () {
Loading picture Resources
bg_on = Bitmapfactory.decoderesource (Getresources (), r.drawable.switch_bkg_switch1);
Bg_off = Bitmapfactory.decoderesource (Getresources (), r.drawable.switch_bkg_switch1);
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 background according to NOWX, on/off status
if (Nowx < (Bg_on.getwidth ()/2)) {
Canvas.drawbitmap (Bg_off, Matrix, paint);//Draw the background when closing
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);//Draw the background when opening
Canvas.drawbitmap (bg_on, Matrix, paint);//Draw the background when opening
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) {//is in a sliding state,
if (nowx >= bg_on.getwidth ())//whether to underline the specified range, not to let the slider run outside, must make this judgment
x = Bg_on.getwidth ()-slipper_btn.getwidth ()/2;//minus the length of thumb 1/2
Else
x = Nowx-slipper_btn.getwidth ()/2;
}else {
if (nowstatus) {//sets the x value of the slider based on the current state
x = Bg_on.getwidth ()-slipper_btn.getwidth ();
}else{
x = 0;
}
}

Exception handling of slider slide, do not let the slider 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 Interface
Invalidate ();
return true;
}



/**
* A method for Wiperswitch to set a listener for external calls
* @param listener
*/
public void Setonchangedlistener (Onchangedlistener listener) {
This.listener = listener;
}

/**
* Set the initial state of the slide switch for external invocation
* @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);
}


}

Android Custom Swipe button

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.