Android Custom View

Source: Internet
Author: User

Custom controls, Video tutorials

Http://www.jikexueyuan.com/course/1748.html

1. Writing the Custom view

2. Join Logical Threads

3. Extracting and encapsulating a custom view

4. Use XML to define styles to influence display effects

----------------------------------

1. Writing the Custom view

Define MyView

 Public classMyViewextendsView { PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicMyView (Context context) {Super(context); } @Overrideprotected voidOnDraw (canvas canvas) {//adding drawing elementsPaint paint =NewPaint (); Paint.settextsize (30); Canvas.drawtext ("Hello Carloz", 0, +, paint);//default left bottom align            }}

Use in Layout file, green background

< Com.carloz.diycontrols.MyView      Android:layout_width = "Match_parent"     android:layout_height= "Match_parent"    android:background= "#00ff00"  />

Run effect

Drawing geometry

Canvas.drawline (0, Max, max, paint);  // Draw a line  New//parm:int//  Draw Rectangle  new// parm:float // Draw a rectangle

Drawing pictures

 Public classMyViewextendsView {Bitmap Bitmap;  PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs); Bitmap=Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher); }     PublicMyView (Context context) {Super(context); Bitmap=Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher); } @Overrideprotected voidOnDraw (canvas canvas) {//adding drawing elementsPaint paint =NewPaint (); Paint.settextsize (30); Canvas.drawtext ("Hello Carloz", 0, +, paint);//default left bottom alignRECTF R=NewRECTF (10, 90, 110, 190);//Parm:intCanvas.drawroundrect (R, ten, G, paint);//Draw rounded rectanglesPaint.setcolor (color.red);//Change the color of a graphicCanvas.drawcircle (max., +, +, paint);//Draw Circle, center, RadiusPaint.setstyle (Style.stroke); //draw a hollow elementcanvas.drawbitmap (Bitmap,(+), and paint);//Drawing    }}

As a contentview, you can see that the background color in the layout file has been removed

 Public class extends Activity {    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);         // Setcontentview (r.layout.activity_main);        Setcontentview (new MyView (this));}    }

2. Join Logical Threads

Purpose: To make the drawing elements move.

2.1 The text of the marquee effect

 Public classLogicviewextendsView {Paint Paint=NewPaint (); String text= "Carloz Logic View"; Private floatRx = 0;        MyThread thread;  PublicLogicview (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub    }     PublicLogicview (Context context) {Super(context); //TODO auto-generated Constructor stub} @Overrideprotected voidOnDraw (canvas canvas) {paint.settextsize (30); Canvas.drawtext (text, RX,30, paint); if(Thread = =NULL) {Thread=NewMyThread ();        Thread.Start (); }    }        classMyThreadextendsThread {@Override Public voidrun () {//TODO auto-generated Method Stub            Super. Run ();  while(true) {Rx+ = 5; if(Rx > GetWidth ())//Let it go when it's out of the screen.Rx = 0-paint.measuretext (text); Postinvalidate (); //The thread updates the drawing and calls the OnDraw method again                Try{Thread.Sleep (50);//It's too fast for the naked eye to see, to sleep.}Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }}

The effect is as follows, the picture does not do well (????)

2.2. Fan Forming Circle

3. Extracting and encapsulating a custom view

4. Use XML to define styles to influence display effects

Android Custom View

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.