1.
2. slidingswitch.java File Contents:
<pre name= "code" class= "java" >package com.jun.widget;import Com.jun.slidingswitch.r;import Android.annotation.suppresslint;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.graphics.rect;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;import android.view.view.ontouchlistener;/** * * Slidingswitch.setcheck (TRUE); * Slidingswitch.setonchangedlistener (new Onchangedlistener () * {* @Override * public void OnChanged (Boolean Check State) * {*//Handle Slidingswitch switch Event *} *}); **/public class Slidingswitch extends View implements Ontouchl istener{private Boolean nowchoose = false;//record whether the current button is open, true to open, flase to close private Boolean isChecked; Private Boolean onsliding = false;//record Whether the user is sliding the variable private float DOWNX, nowx;//pressed x, current x private Rect btn_on, Btn_o ff;//Open and closed, the cursor's rect private Boolean ischglsnon = false; Private Onchangedlistener Chglsn; Private Bitmap bg_on, Bg_off, Sliding_switch; Public Slidingswitch (Context context) {super (context); Init (); } public Slidingswitch (context context, AttributeSet Attrs) {Super (context, attrs); Init (); } public Slidingswitch (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Init (); } private void Init () {//Initialize bg_on = Bitmapfactory.decoderesource (Getresources (), r.drawable.slip_left); Bg_off = Bitmapfactory.decoderesource (Getresources (), r.drawable.slip_right); Sliding_switch = Bitmapfactory.decoderesource (Getresources (), r.drawable.slip_ball); btn_on = new Rect (0, 0, sliding_switch.getwidth (), Sliding_switch.getheight ()); Btn_off = new Rect (Bg_off.getwidth ()-sliding_switch.getwidth (), 0, Bg_off.getwidth (), Sliding_switch.geth EIght ()); Setontouchlistener (this);//Set listener, can also directly replicate ontouchevent} @SuppressLint ("Drawallocation") @Override protected Vo ID onDraw (canvas canvas) {//Drawing function Super.ondraw (canvas); Matrix matrix = new Matrix (); Paint paint = new paint (); float x; if (Nowx < (Bg_on.getwidth ()/2))//slide to the first half and the second half of the background is different, here to make a judgment {x = Nowx-sliding_switch.getwidth ()/2 ; Canvas.drawbitmap (Bg_off, Matrix, paint);//Draw close Background} else {x = Bg_on.getwidth ()-Slidi Ng_switch.getwidth ()/2; Canvas.drawbitmap (bg_on, Matrix, paint);//Draw the open background} if (onsliding)//is in the sliding state, {if (now x >= bg_on.getwidth ())//whether to underline the specified range, cannot let the cursor go outside, must make this judgment x = Bg_on.getwidth ()-Sliding_switch.getwidth ()/2 ;//minus the length of cursor 1/2 ... else if (Nowx < 0) {x = 0; } else {x = NOWX-SLIDING_SWItch.getwidth ()/2; }} else {//non-sliding state if (nowchoose)//The position of the cursor is drawn according to the current switch state {x = Btn_ Off.left; Canvas.drawbitmap (bg_on, Matrix, paint);//The Open state picture should be drawn when the initial state is true} else X = Btn_on.lef T } if (isChecked) {canvas.drawbitmap (bg_on, Matrix, paint); x = Btn_off.left; isChecked =!ischecked; } if (x < 0)//the cursor position is abnormal ... x = 0; else if (x > Bg_on.getwidth ()-Sliding_switch.getwidth ()) x = Bg_on.getwidth ()-sliding_switch.getwidth (); Canvas.drawbitmap (sliding_switch, x, 0, paint);//Draw a travel mark. } @Override public boolean OnTouch (View V, motionevent event) {switch (event.getaction ())//root According to the action to execute code {case motionevent.action_move://Sliding nowx = Event.getx (); Break Case Motionevent.action_down://Press if (Event.getx () > bg_on.getwidth () | | event.gety () > Bg_on.getheight ()) return false; Onsliding = true; Downx = Event.getx (); NOWX = Downx; Break Case Motionevent.action_cancel://Move to the outside of the control onsliding = false; Boolean choose = Nowchoose; if (Nowx >= (Bg_on.getwidth ()/2)) {nowx = Bg_on.getwidth ()-Sliding_switch.getwid Th ()/2; Nowchoose = true; } else {nowx = Nowx-sliding_switch.getwidth ()/2; Nowchoose = false; } if (Ischglsnon && (Choose! = Nowchoose))//If the listener is set, call its method: Chglsn.onchanged (Nowchoose); Break Case motionevent.action_up://Loosen onsliding = false; Boolean LastchOose = Nowchoose; if (Event.getx () >= (Bg_on.getwidth ()/2)) {nowx = Bg_on.getwidth ()-SLIDING_SWITC H.getwidth ()/2; Nowchoose = true; } else {nowx = Nowx-sliding_switch.getwidth ()/2; Nowchoose = false; } if (Ischglsnon && (lastchoose! = nowchoose))//If the listener is set, call its method: Chglsn.onchanged (Nowchoose); Break Default:} invalidate ();//Redraw control return true; The public void Setonchangedlistener (Onchangedlistener l) {//sets the listener when the state is modified Ischglsnon = true; CHGLSN = l; } public interface Onchangedlistener {abstract void OnChanged (Boolean checkstate); The public void SetCheck (Boolean isChecked) {this.ischecked = isChecked; Nowchoose = isChecked; }}
2. Activity_main.xml Layout content:
<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 " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=" com.jun.slidingswitch.MainActivity "> < Com.jun.widget.SlidingSwitch android:layout_width= "wrap_content" android:layout_height= "Wrap_content" /></relativelayout>
3. mainactivity.java File Contents:
package Com.jun.slidingswitch;import Android.support.v7.app.actionbaractivity;import Android.os.Bundle;import Android.view.menu;import Android.view.menuitem;public class Mainactivity extends Actionbaractivity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}
5.demo (Baidu Cloud Network disk)
Http://pan.baidu.com/s/1dDpgqdV
Customizing a switch with sliding properties