Item: Custom view with rounded effect
First, inherit the view and rewrite the OnDraw method
Public classCircleviewextendsview{Private Static Final intCOLOR =color.red; PrivatePaint Mpaint =NewPaint (Paint.anti_alias_flag); Private intMwidth = 0; Private intMheight = 0; PublicCircleview (Context context, AttributeSet attrs) {Super(context, attrs); Init (); } PublicCircleview (Context context) {Super(context); Init (); } PublicCircleview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); Init (); } Private voidinit () {mpaint.setcolor (COLOR); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //Gets the width/height of the current viewMwidth =getmeasuredwidth (); Mheight=getmeasuredheight (); //Get Radius intRadium = Math.min (mwidth,mheight)/2; //Draw a circleCanvas.drawcircle (MWIDTH/2,MHEIGHT/2, Radium,mpaint); }}
Circleview
Testing margin discovery in XML can be used to show that margin is controlled by the parent container (think Measurechildmarginlayout source)
But wrap_content and padding are not effective.
Second, let the wrap_content enter into force
According to the previous Chapter view works: ①, rewrite the Onmeasure method ②, set a fixed width to the Circleview
//set the width of the wrap_content timePrivate Static Final intAt_width = 30;Private Static Final intAt_height = 30;//overriding the Onmeasure () method@Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { //get the scope of a child view intWidthsize =measurespec.getsize (WIDTHMEASURESPEC); intWidthmode =Measurespec.getmode (WIDTHMEASURESPEC); intHeightsize =measurespec.getsize (HEIGHTMEASURESPEC); intHeightmode =Measurespec.getmode (HEIGHTMEASURESPEC); //Judging when the attribute is wrap_content if(Widthmode = = Measurespec.at_most && Heightmode = =measurespec.at_most) {setmeasureddimension (at_width,at_height); } Else if(Widthmode = =measurespec.at_most) {setmeasureddimension (at_width,heightsize); } Else if(Heightmode = =measurespec.at_most) {setmeasureddimension (widthsize,at_height); } Else { Super. Onmeasure (WIDTHMEASURESPEC,HEIGHTMEASURESPEC); } }
Iii. solving problems that cannot be padding
Principle: Only need to be in the OnDraw, get padding parameters can be
//overriding the OnDraw method protected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); //Get padding intPaddingleft =Getpaddingleft (); intPaddingright =getpaddingright (); intPaddingtop =Getpaddingtop (); intPaddingbottom =Getpaddingbottom (); //Gets the width/height minus padding of the current viewMwidth = Getmeasuredwidth ()-Paddingleft-paddingright; Mheight= Getmeasuredheight ()-Paddingtop-Paddingbottom; //Get Radius intRadium = Math.min (mwidth,mheight)/2; //Draw a circleCanvas.drawcircle (PADDINGLEFT+MWIDTH/2,PADDINGTOP-MHEIGHT/2, Radium,mpaint); }
CircleviewIv. Custom Attributes
Step: ①, create the XML file name in the values directory, the file name must start with Attr_. ②, contents of the:<declare-styleadable> tag: name represents a custom attribute (the Circleview Class)
The name in the <attr> tag represents the property name (Circle_color) that is used after, and format is represented by the formatting (color)
<resources> <declare-styleable name= "Circleview" > <attr name= "color_circle" format= "Color" /> </declare-styleable></resources>
Attr_circleview
Step ③, using custom attributes in the layout file must be declared in schemas: xmlns:app= "Http://schemas.android.com/apk/res-auto" where the app name can be replaced casually.
But the prefix for custom attribute names in Circleview must be the same as here (the app is generally used)
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"//This paragraph must be added to the app name can be replaced
Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.maikefengchao.circleview.MainActivity"> <Com.maikefengchao.circleview.CircleViewAndroid:layout_width= "80DP"Android:layout_height= "80DP"
The prefix is consistent with the added declaration prefix
app:color_circle= "#9999"/></LinearLayout>
Step ④: Get custom attribute parameters in Circleview
public int defstyleattr) {Super (context, Attrs, DEFSTYLEATTR); // Load Custom Properties collection Circleview TypedArray a = context.obtainstyledattributes (attrs,r.styleable.circleview); // resolves circle_circle in the collection, sets the default color Mcolor = A.getcolor (r.styleable.circleview_color_circle,color.red); Init (); }
All code: (P209①)
Custom View one (Inherit view override OnDraw method)