Android Custom Controls

Source: Internet
Author: User

 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

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.