The controls provided by Android sometimes do not meet the needs of our projects, so we need to customize the view style and event monitoring.
To customize the view:
1. Custom View Properties
2. Get our custom properties in the View construction method
3. Rewrite onmesure
4. Rewrite OnDraw
3 of them are not necessarily necessary, and of course most of the cases need to be rewritten.
The following is an analysis by implementing a circle to show progress.
1> the properties of a custom view, first create a attrs.xml under res/values/.
<?xml version= "1.0" encoding= "Utf-8"?><resources> <attr name= "firstcolor" format= "Color"/> <attr name= "secondcolor" format= "color"/> <attr name= "Speed" format= "integer"/> <attr Name= "Circlewidth" format= "Dimension"/> <declare-styleable name= "Circleview" > <attr name= " Firstcolor "/> <attr name=" Secondcolor "/> <attr name=" Speed "/> <attr name= " Circlewidth "/> </declare-styleable></resources>
2> declaring a custom view in a layout
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Xmlns:custom= "Http://schemas.android.com/apk/res-auto"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"Tools:context= "Com.example.selfview.MainActivity" > <Com.example.selfview.CircleView android:layout_gravity= "Center"Android:layout_width= "200DP"Android:layout_height= "200DP"Custom:firstcolor= "#FFF0"Custom:secondcolor= "#000"Custom:circlewidth= "20SP"Custom:speed= "Ten"/></linearlayout>
3> in the method of customizing view, get our style and paint. 3 constructor methods are overridden, and the default layout file calls the two-parameter construction method.
Public classCircleviewextendsView {Private intFirstcolor; Private intSecondcolor; Private intSpeed ; Private intCriclewidth; PrivatePaint Mpaint; Private intmprogress; PublicCircleview (Context context) { This(Context,NULL); } PublicCircleview (Context context, AttributeSet attrs) { This(Context, attrs,0); } PublicCircleview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); TypedArray a= Context.gettheme (). Obtainstyledattributes (Attrs, R.styleable.circleview, defstyleattr, 0); Firstcolor=A.getcolor (R.styleable.circleview_firstcolor, Color.green); Secondcolor=A.getcolor (r.styleable.circleview_secondcolor,color.red); Criclewidth= A.getdimensionpixelsize (R.styleable.circleview_circlewidth, (int) Typedvalue.applydimension (typedvalue.complex_unit_px,20, Getresources (). Getdisplaymetrics ()); speed= A.getint (r.styleable.circleview_speed,20); A.recycle (); NewThread (NewRunnable () {@Override Public voidrun () { while(true){ Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); } if(Mprogress <= 360) {mprogress+=Speed ; }Else{thread.interrupted (); }}}). Start (); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); intCentre = getwidth ()/2; intRadius = CENTRE-CRICLEWIDTH/2; Mpaint=NewPaint (); Mpaint.setstrokewidth (Criclewidth); //set the width of the ringMpaint.setantialias (true);//Anti- aliasingMpaint.setstyle (Paint.Style.STROKE);//Set HollowRECTF Oval =NewRECTF (Centre-radius, Centre-radius, centre + radius, centre +radius); Mpaint.setcolor (Firstcolor); //set the color of the ringCanvas.drawcircle (Centre, centre, Radius, mpaint);//Draw a circleMpaint.setcolor (Secondcolor);//set the color of the ringCanvas.drawarc (oval, -90, mprogress,false, Mpaint);//draw arcs based on progressLOG.D ("Azheng", String.valueof (mprogress)); Invalidate (); }}
4> because we are static custom view, we do not need to call in Mainactivity.
Public class extends appcompatactivity { @Override protectedvoid onCreate (Bundle Savedinstancestate) { super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); }}
Here, our instance is ready to run.
We have not rewritten the Onmeasure method and, if necessary, can adjust the display of our view by this method. Of course, we can also set the event listener for a custom view, such as in the construction method
This.setonclicklistener () realizes tap listening.
Reference:
http://blog.csdn.net/lmj623565791/article/details/24252901/
Android Custom View