Packagecom.example.app.sw.kg;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Toast; Public classMyswitchbtnviewextendsViewImplementsonclicklistener{PrivatePaint paint =NewPaint (Paint.anti_alias_flag); Private BooleanFlag =false ; PrivateContext Mcontext; Bitmap Bitmap_open=NULL ; Bitmap Bitmap_close=NULL; PublicMyswitchbtnview (Context context, AttributeSet attrs) {Super(context, attrs); Mcontext=context; Init (); } Private voidinit () {Setonclicklistener ( This); Bitmap_open=Bitmapfactory.decoderesource (Mcontext.getresources (), r.drawable.btn_open); Bitmap_close=Bitmapfactory.decoderesource (Mcontext.getresources (), r.drawable.btn_close); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {setmeasureddimension (Bitmap_open.getwidth (), Bitmap_close.getheight ()); } PublicMyswitchbtnview (Context context) {Super(context); Mcontext=context; Init (); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); if(flag) {//Draw OpenCanvas.drawbitmap (bitmap_open, 0, 0, paint); Toast.maketext (Mcontext,"was opened", 0). Show (); }Else{ //Draw offCanvas.drawbitmap (bitmap_close, 0, 0, paint); Toast.maketext (Mcontext,"was shut down", 0). Show (); }} @Override Public voidOnClick (View v) {if(flag) {flag=false ; }Else{flag=true ; } invalidate (); //Refresh Screen }}
Android Custom Controls