[Java] view plaincopy
[Java]
The java class is as follows:
[Java]
[Java]
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. porterduduxfermode;
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 context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
Init (context, attrs );
}
Public RoundAngleImageView (Context context, AttributeSet attrs ){
Super (context, attrs );
Init (context, attrs );
}
Public RoundAngleImageView (Context context ){
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 porterduxfermode (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,
GetWidth ()),
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 an attr. xml file and put it under the values directory. The content is 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>
Example:
Declare the attribute namespace first:
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