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