Recently made a UI effect that requires the avatar to be round, with a white border outside
Students who need to take a look, the effect is as follows
Attach implementation Code
Roundimageview:
Public classRoundimageviewextendsImageView { PublicRoundimageview (Context context, AttributeSet attrs) {Super(context, attrs); Init (); } PublicRoundimageview (Context context) {Super(context); Init (); } Private FinalRECTF RoundRect =NewRECTF (); Private floatRect_adius = 90; Private FinalPaint Maskpaint =NewPaint (); Private FinalPaint Zonepaint =NewPaint (); Private voidinit () {Maskpaint.setantialias (true); Maskpaint.setxfermode (NewPorterduffxfermode (PorterDuff.Mode.SRC_IN)); Maskpaint.setfilterbitmap (true); Zonepaint.setantialias (true); Zonepaint.setcolor (Color.White); Zonepaint.setfilterbitmap (true); floatDensity =getresources (). Getdisplaymetrics (). density; Rect_adius= Rect_adius *density; } Public voidSetrectadius (floatAdius) {Rect_adius=Adius; Invalidate (); } @Overrideprotected voidOnLayout (BooleanChangedintLeftintTopintRight ,intbottom) { Super. OnLayout (changed, left, top, right, bottom); intW =getwidth (); inth =getheight (); Roundrect.set (0, 0, W, h); } @Override Public voidDraw (canvas canvas) {Canvas.savelayer (RoundRect, Zonepaint, Canvas.all_save_flag); Canvas.drawroundrect (RoundRect, Rect_adius, Rect_adius, Zonepaint); //Canvas.savelayer (RoundRect, Maskpaint, Canvas.all_save_flag); Super. Draw (canvas); Canvas.restore (); }}
XML file:
<relativelayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:layout_centervertical= "true"android:gravity= "Center_vertical" > <Com.example.view.RoundImageView Android:id= "@+id/img_user_icon"Android:layout_width= "100DP"Android:layout_height= "100DP"Android:layout_centerhorizontal= "true"Android:scaletype= "Centercrop"android:src= "@drawable/user_icon"/> <ImageView android:layout_width= "100DP"Android:layout_height= "100DP"Android:layout_centerhorizontal= "true"Android:background= "@drawable/usericon_bg_circle"/> </RelativeLayout>
drawable file:
Usericon_bg_circle.xml
<?xml version= "1.0" encoding= "UTF-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" android:shape= "Rectangle" > <solid android:color= "#00000000"/> <corners Android : radius= "90DP"/> <stroke android:width= "2DP" android:color= "#fff"/>< /shape>
Android implementation round ImageView with white border