Getting Started with Android-android custom controls

Source: Internet
Author: User

The first type: Inherit view

To implement your own properties

<Com.cc.imagewithmarkersample.MyView Android:id="@+id/myviewid"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_centerhorizontal="true"SRCLT="@drawable/red"SRCRT="@drawable/green"SRCRB="@drawable/green"/>
 Public classMyView extends View {//private String Mtext;    Private intmsrclt, MSRCRT, MSRCRB; Private StaticFinal String SRCLT ="SRCLT"; Private StaticFinal String SRCRT ="SRCRT"; Private StaticFinal String SRCRB ="SRCRB"; Private StaticFinalintcanvas_w= Max, canvas_h= Max; Private StaticFinalintrect_w= -, rect_h= -; PrivateBitmap Bitmap; Private intBitmap_w,bitmap_h; Private intlt_x=0, lt_y=0; Private intrt_x= -, rt_y=0; Private intrb_x= -, rb_y= -; Private intbitmap_x= -, bitmap_y= -; PrivateRect mrect=NULL;  PublicMyView (Context context) {super (context); }     PublicMyView (Context context, AttributeSet Attrs) {Super (context, attrs);//int textid = Attrs.getattributeresourcevalue (null, "Text", 0);        intSrclefttopid = Attrs.getattributeresourcevalue (NULL, SRCLT,0); intSrcrighttopid = Attrs.getattributeresourcevalue (NULL, SRCRT,0); intSrcrightbottomid = Attrs.getattributeresourcevalue (NULL, SRCRB,0);//Mtext = Context.getresources (). GetText (Textid). toString ();MSRCLT =Srclefttopid; MSRCRT=Srcrighttopid; MSRCRB=Srcrightbottomid; Mrect=NewRect (bitmap_x,bitmap_y,rect_w+bitmap_x,rect_h+bitmap_y); }     Public voidSetimagebitmap (Bitmap BM) {/** Get Picture Height **/Bitmap_w=bm.getwidth (); Bitmap_h=bm.getheight (); Bitmap_x= (canvas_w-bitmap_w)/2; Bitmap_y= (canvas_h-bitmap_h)/2; if(Bitmap! =BM) {Bitmap=m; }} @Overrideprotected voidOnDraw (canvas canvas) {Paint Paint=NewPaint ();        Paint.setcolor (Color.gray);        Canvas.drawbitmap (Bitmap, bitmap_x, bitmap_y, paint); Paint.setalpha ( -);        Canvas.drawrect (Mrect,paint); Paint.setalpha (255);        ONDRAWLT (canvas, paint);        Ondrawrt (canvas, paint); ONDRAWRB (canvas, paint);//Canvas.drawtext (Mtext, BW/2, (), paint);    }    Private voidondrawlt (canvas canvas, paint paint) {InputStream is=getresources (). Openrawresource (MSRCLT); Bitmap Mbitmap= Bitmapfactory.decodestream ( is); intBH =mbitmap.getheight (); intBW =mbitmap.getwidth ();    Canvas.drawbitmap (Mbitmap, lt_x, lt_y, paint); }    Private voidondrawrt (canvas canvas, paint paint) {InputStream is=getresources (). Openrawresource (MSRCRT); Bitmap Mbitmap= Bitmapfactory.decodestream ( is); intBH =mbitmap.getheight (); intBW =mbitmap.getwidth ();    Canvas.drawbitmap (Mbitmap, rt_x, rt_y, paint); }    Private voidONDRAWRB (canvas canvas, paint paint) {InputStream is=getresources (). Openrawresource (MSRCRB); Bitmap Mbitmap= Bitmapfactory.decodestream ( is); intBH =mbitmap.getheight (); intBW =mbitmap.getwidth ();    Canvas.drawbitmap (Mbitmap, rb_x, rb_y, paint); }}

Getting Started with Android-android custom controls

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.