Android custom sliding switch control, suitable for all android Systems

Source: Internet
Author: User

We all know that a sliding switch is provided for Android 4.0 and later versions. Many people choose to use checkbox and put two images below. However, this operation only applies, and the effect is not very good, so I have customized the sliding switch wiperswitch control. paste it next. This is a really bad picture. You can change it to three pictures by yourself.

Package COM. example. wiperswitch; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; 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;/***** @ author xiaanming ***/public class WIP Erswitch extends view implements ontouchlistener {private bitmap bg_on, bg_off, slipper_btn;/*** 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) {super (context); Init ();} public wiperswitch (context, attributeset attrs) {Su Per (context, attrs); Init ();} public void Init () {// load image resource bg_on = bitmapfactory. decoderesource (getresources (), R. drawable. on_btn); bg_off = bitmapfactory. decoderesource (getresources (), R. drawable. off_btn); slipper_btn = bitmapfactory. decoderesource (getresources (), R. drawable. white_btn); setontouchlistener (this);} protected void ondraw (canvas) {super. ondraw (canvas); matrix = new matrix (); pain T paint = new paint (); float x = 0; // set the background, on or off status if (nowx <(bg_on.getwidth ()/2) {canvas. drawbitmap (bg_off, matrix, paint); // draws the background when it is disabled} else {canvas. drawbitmap (bg_on, matrix, paint); // draws the background when open} If (onslip) {// whether it is in the sliding state, if (nowx> = bg_on.getwidth ()) // specifies whether to specify a range. The slider cannot be redirected to the external header. You must determine x = bg_on.getwidth ()-slipper_btn.getwidth ()/2; // subtract the length of the slider 1/2 elsex = nowx-slipper_btn.getwidth ()/2;} else {If (nowstatus) {// Root Set the X value of the slider to X = bg_on.getwidth ()-slipper_btn.getwidth () ;}else {x = 0 ;}// handle the exception of the slider slide, cannot expose 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) ;}@ overridepublic 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 invalidate (); Return true ;}/ *** to set a listener for the wiperswitch, method for external call * @ Param listener */Public void setonchangedlistener (onchangedlistener listener) {This. listener = listener;}/*** sets 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, Boolean checkstate );}}

The usage is to first define the XML file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.example.wiperswitch.WiperSwitch        android:id="@+id/wiperSwitch1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>

Create an activity

Package COM. example. wiperswitch; import android. app. activity; import android. OS. bundle; import android. util. log; import COM. example. wiperswitch. wiperswitch. onchangedlistener; public class mainactivity extends activity implements onchangedlistener {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); // instantiate wiperswitchwiperswitch wiperswitch = (wiperswitch) findviewbyid (R. id. wiperswitswitch); // sets the initial state to falsewiperswitch. setchecked (false); // sets the listener wiperswitch. setonchangedlistener (this) ;}@ overridepublic void onchanged (wiperswitch, Boolean checkstate) {log. E ("log", "" + checkstate );}}

The code is all over. You are welcome to give me some advice on poor writing!

Oh, I forgot that there were still three bad pictures not passed.

 

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.