Android Custom Rounded Corners imageview_android

Source: Internet
Author: User

No more nonsense to say, directly to everyone paste code.

The Java classes are as follows:

Import Android.content.Context; 
Import Android.content.res.TypedArray; 
Import Android.graphics.Bitmap; 
Import Android.graphics.Bitmap.Config; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import Android.graphics.Paint; 
Import Android.graphics.Path; 
Import Android.graphics.PorterDuff; 
Import Android.graphics.PorterDuffXfermode; 
Import Android.graphics.RectF; 
Import Android.util.AttributeSet; 
Import Android.widget.ImageView; 
Import CN.DOTCREATE.TT.R; 
public class Roundangleimageview extends ImageView {private Paint Paint; 
private int roundwidth = 5; 
private int roundheight = 5; 
Private Paint Paint2; 
Public Roundangleimageview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
Init (context, attrs); 
Public Roundangleimageview (context, AttributeSet attrs) {Super (context, attrs); 
Init (context, attrs); 
Public Roundangleimageview {Super (context); 
Init (context, NULL); } Private VOID init (Context context, AttributeSet attrs) {if (attrs!= null) {TypedArray a = Context.obtainstyledattributes (Attrs, 
R.styleable.roundangleimageview); 
Roundwidth= a.getdimensionpixelsize (R.styleable.roundangleimageview_roundwidth, roundWidth); 
Roundheight= a.getdimensionpixelsize (R.styleable.roundangleimageview_roundheight, roundHeight); 
}else {Float density = context.getresources (). Getdisplaymetrics (). density; 
Roundwidth = (int) (roundwidth*density); 
Roundheight = (int) (roundheight*density); 
} paint = new paint (); 
Paint.setcolor (Color.White); 
Paint.setantialias (TRUE); 
Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_OUT)); 
Paint2 = new Paint (); 
Paint2.setxfermode (NULL); @Override public void Draw (Canvas Canvas) {Bitmap Bitmap = Bitmap.createbitmap (GetWidth (), GetHeight (), config.argb_ 
8888); 
Canvas canvas2 = new Canvas (bitmap); 
Super.draw (CANVAS2); 
Drawliftup (CANVAS2); 
Drawrightup (CANVAS2); 
Drawliftdown (CANVAS2); 
Drawrightdown (CANVAS2); CanVas.drawbitmap (bitmap, 0, 0, Paint2); 
Bitmap.recycle (); 
private void Drawliftup (Canvas Canvas) {Path PATH = new Path (); 
Path.moveto (0, roundheight); 
Path.lineto (0, 0); 
Path.lineto (roundwidth, 0); 
Path.arcto (New RECTF (0, 0, roundwidth*2, roundheight*2),-90,-90); 
Path.close (); 
Canvas.drawpath (path, paint); 
private void Drawliftdown (Canvas Canvas) {Path PATH = new Path (); 
Path.moveto (0, GetHeight ()-roundheight); 
Path.lineto (0, GetHeight ()); 
Path.lineto (Roundwidth, GetHeight ()); 
Path.arcto (New RECTF (0, GetHeight ()-roundheight*2, 0+roundwidth*2, GetHeight ()), 90, 90); 
Path.close (); 
Canvas.drawpath (path, paint); 
private void Drawrightdown (Canvas Canvas) {Path PATH = new Path (); 
Path.moveto (GetWidth ()-roundwidth, getheight ()); 
Path.lineto (GetWidth (), getheight ()); 
Path.lineto (GetWidth (), getheight ()-roundheight); 
Path.arcto (New RECTF (GetWidth ()-roundwidth*2, getheight ()-roundheight*2, getwidth (), GetHeight ()), 0, 90); 
Path.close (); CanVas.drawpath (path, paint); 
private void Drawrightup (Canvas Canvas) {Path PATH = new Path (); 
Path.moveto (GetWidth (), roundheight); 
Path.lineto (getwidth (), 0); 
Path.lineto (GetWidth ()-roundwidth, 0); 
Path.arcto (New RECTF (GetWidth ()-roundwidth*2, 0, GetWidth (), 0+roundheight*2),-90, 90); 
Path.close (); 
Canvas.drawpath (path, paint);  } 
}

Define a Attr.xml file, placed under the values directory, as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= " Roundangleimageview ">
<attr name=" Roundwidth "format=" Dimension "/> <attr" name= roundheight "
format= "Dimension"/>
</declare-styleable>
</resources>

Use the example below:

First, declare the name space of the property:

And then write the same as the general definition view:

<cn.dotcreate.tt.ui.roundangleimageview
android:id= "@+id/headiv"
android:layout_width= "75DP
" android:layout_height= "75DP"
android:layout_centervertical= "true"
android:layout_marginleft= "2DP"
app:roundwidth= "10DP"
app:roundheight= "10DP"
android:src= "@drawable/default_head_icon"/>

Effect as shown:

The above code briefly introduces the Android custom rounded corner imageview knowledge, hope this article to share to everybody to be helpful.

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.