Android implementation round ImageView with white border

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.