Let's look at the slide switch first:
Let's start with the code:
Here is the custom control Togglebutton.java:
PackageCom.fay.toggle;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.View;/*** Toggle the status *@since2014/05/22 *@authorFay * {@link[email protected]}*/ Public classToggleButtonextendsView {PrivateString TAG = "ToggleButton"; //The bitmap of toggle on PrivateBitmap Backgroudbitmap =NULL; //The bitmap of toggle Flip PrivateBitmap Slidingbitmap =NULL; //Whether is button if is Sliding Private BooleanIssliding =false; //The previous state of the button Private BooleanPreviousstate =false; PrivatePaint Mpaint =NewPaint (); PrivateMatrix Mmatrix =NewMatrix (); PrivateOntogglestatechangedlistener Montogglestatechangedlistener =NULL; //Current x-location which touched Private floattouchxlocation = 0; //The slidingbitmap inner margin The ToggleButton Private floatMarginLeft = 0; PublicToggleButton (Context context) {Super(context); } PublicToggleButton (Context context, AttributeSet attrs) {Super(context, attrs); } PublicToggleButton (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); } /*** Set the background for the ToggleButton and sliding image resource *@paramint Backgroudresid *@paramint Flipresid*/ Public voidSetimageresource (intBackgroudresid,intFlipresid) {Backgroudbitmap=Bitmapfactory.decoderesource (Getresources (), backgroudresid); Slidingbitmap=Bitmapfactory.decoderesource (Getresources (), flipresid); } /*** Set the Initialize state of the view *@paramBoolean isOn*/ Public voidSetinitstate (BooleanisOn) { Previousstate=IsOn; } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { //TODO auto-generated Method Stub Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); } @Overrideprotected voidOnDraw (canvas canvas) {canvas.drawbitmap (Backgroudbitmap, Mmatrix, Mpaint); if(issliding) {//if sliding//To avoid slidingbitmap sliding out of the ToggleButton if(Touchxlocation >= backgroudbitmap.getwidth ()-Slidingbitmap.getwidth ()/2 | | touchxlocation <= Slidingbitmap.getwidth ()/2) { if(Touchxlocation >= backgroudbitmap.getwidth ()-Slidingbitmap.getwidth ()/2) {marginleft= Backgroudbitmap.getwidth ()-slidingbitmap.getwidth (); } Else{marginleft= 0; } } Else{marginleft= Touchxlocation-slidingbitmap.getwidth ()/2; } canvas.drawbitmap (Slidingbitmap, MarginLeft,0, Mpaint); } Else { if(Previousstate = =true) {// onCanvas.drawbitmap (Slidingbitmap, Backgroudbitmap.getwidth ()-slidingbitmap.getwidth (), 0, Mpaint); } Else{canvas.drawbitmap (Slidingbitmap,0, 0, Mpaint); } } Super. OnDraw (canvas); } @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { CaseMotionevent.action_down:if(0 <= Event.getx () && event.getx () <=backgroudbitmap.getwidth ()&& 0 <= event.gety () && event.gety () <=backgroudbitmap.getheight ()) {touchxlocation=Event.getx (); Issliding=true; } Else{issliding=false; } Break; CaseMotionevent.action_move:if(issliding) {//To avoid change the state out of the toggleTouchxlocation =Event.getx (); } Break; CaseMotionEvent.ACTION_UP:isSliding=false; if(Touchxlocation > Backgroudbitmap.getwidth ()/2) {// on//If previous state is off if(Previousstate = =false) {montogglestatechangedlistener.changed (true); Previousstate=true; } } Else if(Touchxlocation < Backgroudbitmap.getwidth ()/2) {//off//if previous state if on if(Previousstate = =true) {montogglestatechangedlistener.changed (false); Previousstate=false; } } Break; } invalidate (); return true; } /*** The Listener of this ToggleButton*/ Public InterfaceOntogglestatechangedlistener {voidChangedBooleanisOn); } /*** Set the Listener for the ToggleButton*/ Public voidSetonstatechangedlistener (Ontogglestatechangedlistener montogglestatechangedlistener) { This. Montogglestatechangedlistener =Montogglestatechangedlistener; }}
Then we look at the layout of this activity activity_main.xml:
<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" tools:context= ". Mainactivity "> <com.fay.toggle.ToggleButton android:id=" @+id/toggle " Android : Layout_width= "Wrap_content" android:layout_height= "Wrap_content" android: Layout_marginleft= "50DP" android:layout_marginright= "50DP" > </ Com.fay.toggle.togglebutton></relativelayout>
And then we use this activity for this control Mainactivity.java
PackageCom.fay.toggle;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.Toast;ImportCom.fay.toggle.ToggleButton.OnToggleStateChangedListener; Public classMainactivityextendsActivity {PrivateToggleButton Mtogglebutton =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mtogglebutton=(ToggleButton) Findviewbyid (R.id.toggle); Mtogglebutton.setinitstate (false); Mtogglebutton.setimageresource (R.drawable.bkg_switch, R.drawable.btn_slip); Mtogglebutton.setonstatechangedlistener (NewOntogglestatechangedlistener () {@Override Public voidChangedBooleanisOn) {Toast.maketext (Getapplicationcontext (), IsOn+ "", 2000). Show (); } }); }}
As you can see, the use of ToggleButton in Mainactivity.java is very simple and convenient. This control is drawn by integrating the view, overriding the OnDraw () method inside. and setting up the listener. Because the usage is very simple, I don't need to be wordy. There is something wrong, I hope you friends in time to give me your valuable advice!