I. ringview
The constructor must overwrite the custom view. For the method to override, refer to the following:
① If you need to change the image drawn by the view, you need to override the ondraw method. (This is also the most common rewrite method .)
② If you need to change the view size, you need to override the onmeasure method.
③ If you need to change the position of the view (in the parent control), you need to override the onlayout method.
④ You can combine multiple rewrite schemes based on the three different requirements above, you know.
The code for commenting information is more detailed.
Package COM. example. customerviewdemo2; import android. content. context; import android. graphics. canvas; import android. graphics. paint; import android. util. attributeset; import android. view. view; public class ringview extends view {private final paint; private final context; Public ringview (context) {This (context, null);} public ringview (context, attributeset attrs) {super (context, attrs); this. context = context; this. paint = new paint (); this. paint. setantialias (true); // remove the Sawtooth this. paint. setstyle (paint. style. stroke); // draw a hollow circle} @ overrideprotected void ondraw (canvas) {// todo auto-generated method stubint center = getwidth ()/2; int innercircle = dip2px (context, 83); // set the inner circle radius int ringwidth = dip2px (context, 10); // set the ring width // draw the inner circle this. paint. setargb (155,167,190,206); this. paint. setstrokewidth (10); // set the thickness of the inner circle canvas. drawcircle (Center, Center, innercircle, this. paint); // use this circle as the radius to extend to the inside and outside to the thickness of 10px // draw the ring, set the color of the ring to modify the color of the paint brush // This. paint. setargb (255,212,225,233); this. paint. setargb (255,255, 0, 0); this. paint. setstrokewidth (ringwidth); // set the ring width canvas. drawcircle (Center, Center, innercircle + 1 + ringwidth/2, this. paint); // The ring width is the center circle // draw the outer circle this. paint. setargb (155,167,190,206); this. paint. setstrokewidth (2); canvas. drawcircle (Center, Center, innercircle + ringwidth, this. paint); super. ondraw (canvas);}/*** convert the unit from Dp to PX (pixel) */public static int dip2px (context, float dpvalue) based on the cell phone resolution) {final float scale = context. getresources (). getdisplaymetrics (). density; Return (INT) (dpvalue * scale + 0.5f );}}
Ii. Code for referencing the view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.example.customerviewdemo2.RingView android:layout_width="300dp" android:layout_height="300dp" > </com.example.customerviewdemo2.RingView></RelativeLayout>
3. The effect is as follows:
Create a circle for custom View