Introduced
Rounded controls are commonly used for avatars, buttons, icons, etc., and are used in a wide variety of applications, often in conjunction with board.
In iOS, UIView's Calayer layer already provides a way to fillet and board, so a rounded control can be made very simple, just like the following simple code:
View.layer.cornerRadius =; View.layer.borderColor = [Uicolor Yellowcolor]. Cgcolor; View.layer.borderWidth = ten; View.clipstobounds = YES;
In Android, the system does not provide such a direct way, so you want to achieve the same effect requires a snack.this articleWill beintroduction of a method for implementing rounded controls with board properties in Android。 This method defines a roundimageview rewrite of the ImageView OnDraw method, and adds some custom attributes, and finally the code to achieve the same effect is very concise:
<com.me.obo.circleboardimage.circleview.roundimageview android:layout_width= "80DP" android:layout_ height= "80DP" android:src= "@drawable/head" attr:border_width= "3DP" attr:corner_y= "40DP" attr: corner_x= "40DP" attr:border_color= "#FFFFFF"/>
The following describes how to implement such a Roundimageview
Matte Cover
In Android, masking is very powerful and can be used to generate two images that intercept each other.
The effects of specific parameters and masks can be found in the link: Android implementation matte
In Roundimageview, the matte is used to clear the contents of the outer ring of the control, thereby obtaining the effect of the fillet. The idea here is to get the picture drawn by the control itself, then get the rounded rectangle matte picture, and then two images using the dst_in mask mode to intercept the middle matte effect, namely:
The code is implemented as follows:
Get ImageView Original picture Super.ondraw (Mdestcanvas); Creates a rectangle that represents the fillet rectangle if (mroundrectclip = = null) { mroundrectclip = new RECTF (Mbordervalue, Mbordervalue, GetWidth ()-M Bordervalue, GetHeight ()-Mbordervalue); } Draw Rounded Rectangle msrccanvas.drawroundrect (mroundrectclip,mcornerxvalue,mcorneryvalue,mnomalpaint); Use a matte brush to deduct portions of the Mdestcanvas.drawbitmap (msrcbitmap,0,0,mpaintclip) outside the rounded rectangle in the original image;
After you get a picture of the matte processing, adding a board to it will be easy, which is to draw a circle with a width in the outer circle.
The code is implemented as follows:
Create Board Rectangle if (Mroundrectborder = = null) { Mroundrectborder = new RECTF (MBORDERVALUE/2, MBORDERVALUE/2, get Width ()-MBORDERVALUE/2, GetHeight ()-MBORDERVALUE/2); } Draw Board Mdestcanvas.drawroundrect (Mroundrectborder, Mcornerxvalue, Mcorneryvalue, Mpaintboard);
The entire Roundimageview complete code is as follows:
Package Com.me.obo.circleboardimage.circleview;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.bitmap;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.porterduff;import Android.graphics.porterduffxfermode;import Android.graphics.rectf;import Android.util.attributeset;import Android.widget.imageview;import com.me.obo.circleboardimage.r;/** * Created by OBO on 16/5/11. * email:[email protected] * git:https://github.com/obobear * Blog:http://blog.csdn.net/leilba */public class Roundimageview extends ImageView {private float mcornerxvalue; private float Mcorneryvalue; private float Mbordervalue; private int mbordercolor; Public Roundimageview (context context, AttributeSet Attrs) {Super (context, attrs); Get custom parameters TypedArray TypedArray = context.obtainstyledattributes (Attrs, R.styleable.roundimageview) ; Board Color Mbordercolor = Typedarray.getcolor (R.styleable.roundimageview_border_color,color.argb (0,0,0,0)); Get x-direction curvature mcornerxvalue = Typedarray.getdimension (r.styleable.roundimageview_corner_x,0); Gets the y-direction curvature mcorneryvalue = Typedarray.getdimension (r.styleable.roundimageview_corner_y,0); board Width mbordervalue = typedarray.getdimension (r.styleable.roundimageview_border_width,0); Run out need recycle typedarray.recycle (); Initpaints (); } private void Initpaints () {//Create normal brush if (Mnomalpaint = = null) {Mnomalpaint = new Paint (); }//Create matte brush if (Mpaintclip = = null) {Mpaintclip = new Paint (); Mpaintclip.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_IN)); Mpaintclip.setantialias (TRUE); }//Create board brush if (Mpaintboard = = null) {Mpaintboard = new Paint (); Mpaintboard.setcolor (Mbordercolor);Mpaintboard.setstyle (Paint.Style.STROKE); Mpaintboard.setstrokewidth (Mbordervalue); }} Bitmap Mdestbitmap; Bitmap Msrcbitmap; Canvas Mdestcanvas; Canvas Msrccanvas; Paint Mpaintclip; Paint Mnomalpaint; Paint Mpaintboard; RECTF Mroundrectclip; RECTF Mroundrectborder; @Override protected void OnDraw (canvas canvas) {//create matte picture and canvas if (Mdestbitmap = = null) {Mdest Bitmap = Bitmap.createbitmap (GetWidth (), GetHeight (), Bitmap.Config.ARGB_8888); Msrcbitmap = Bitmap.createbitmap (GetWidth (), GetHeight (), Bitmap.Config.ARGB_8888); Mdestcanvas = new Canvas (MDESTBITMAP); Msrccanvas = new Canvas (MSRCBITMAP); }//Get ImageView original picture Super.ondraw (Mdestcanvas); Creates a rectangle that represents the rounded rectangle if (mroundrectclip = = null) {Mroundrectclip = new RECTF (Mbordervalue, Mbordervalue, GETW Idth ()-Mbordervalue, GetHeight ()-mbordervalue); }//Draw rounded Rectangle MSrCcanvas.drawroundrect (Mroundrectclip,mcornerxvalue,mcorneryvalue,mnomalpaint); Use a matte brush to deduct portions of the Mdestcanvas.drawbitmap (Msrcbitmap,0,0,mpaintclip) outside the rounded rectangle in the original image; Create Board Rectangle if (Mroundrectborder = = null) {Mroundrectborder = new RECTF (MBORDERVALUE/2, Mbordervalu E/2, GetWidth ()-MBORDERVALUE/2, GetHeight ()-MBORDERVALUE/2); }//Draw board Mdestcanvas.drawroundrect (Mroundrectborder, Mcornerxvalue, Mcorneryvalue, Mpaintboard); Draw the final fillet with board Figure Canvas.drawbitmap (Mdestbitmap,0,0,mnomalpaint); }}
Custom attribute
One might ask how the fillet curvature and board parameters are passed into the Roundimageview. In fact, the custom attribute is used here, which requires creating a Attr.xml file in the Res/values directory, with a custom parameter name and type:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "Roundimageview" > <attr name= "Border_width" format= "Dimension"/> <attr name= "border_color" format= "Color"/> <attr name= "corner_x" format= "Dimension"/> <attr name= "corner_y" format= "Dimension"/> </declare-styleable></resources>
With this, you can get the value of the custom Attri in the Roundimageview constructor.
Public Roundimageview (context context, AttributeSet Attrs) { Super (context, attrs); Gets the custom parameter TypedArray TypedArray = context.obtainstyledattributes (attrs, r.styleable.roundimageview); board color mbordercolor = Typedarray.getcolor (R.styleable.roundimageview_border_color,color.argb (0,0,0,0)); Get x-direction curvature Mcornerxvalue = typedarray.getdimension (r.styleable.roundimageview_corner_x,0); Gets the y-direction curvature Mcorneryvalue = typedarray.getdimension (r.styleable.roundimageview_corner_y,0); Board width mbordervalue = typedarray.getdimension (r.styleable.roundimageview_border_width,0); Run out need recycle typedarray.recycle (); Initpaints (); }
When used in layout, you need to apply a custom parameter to the beginning of the XML layout.
Xmlns:attr= "Http://schemas.android.com/apk/res-auto"
Finally, the code that calls Roundimageview in layout is very concise:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:attr=" Http://schemas.android.com/apk/res-auto "android:layout_width=" Match_parent "Android:layout_ height= "Match_parent" android:background= "@android: Color/darker_gray" android:orientation= "vertical" > <com. Me.obo.circleboardimage.circleview.RoundImageView android:layout_width= "80DP" android:layout_height= "80DP" android:src= "@drawable/head" attr:border_width= "3DP" attr:corner_y= "40DP" attr:corner_x= "40DP" Attr:border_color= "#FFFFFF"/> <com.me.obo.circleboardimage.circleview.roundimageview android:layout _width= "180DP" android:layout_height= "180DP" android:src= "@drawable/head" attr:border_width= "2DP" Attr:corner_y= "40DP" attr:corner_x= "40DP" attr:border_color= "#FF0000" android:scaletype= "Fitxy"/& Gt </LinearLayout>
The effect is as follows:
Resources download
Roundimageview
"Android" Android custom rounded control with board