Android custom rounded corner ImageView and androidimageview
We often see some apps that can display rounded corner images, such as qq contact icons. One way to achieve rounded corner images is to directly use the rounded corner image resources. Of course, if there is no rounded corner image resources, we can also implement it through a program. The following describes how to customize the ImageView of the rounded corner:
Package com. yulongfei. imageview; 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. porterduxfermode; import android. graphics. rectF; imp Ort android. util. attributeSet; import android. widget. imageView; public class RoundAngleImageView extends ImageView {private int roundWidth = 13; private int roundHeight = 13; public RoundAngleImageView (Context context) {super (context); init (context, null );} public RoundAngleImageView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init (context, attrs);} public RoundAng LeImageView (Context context, AttributeSet attrs) {super (context, attrs); init (context, attrs);} private void init (Context context, AttributeSet attrs) {if (attrs! = Null) {TypedArray a = context. obtainStyledAttributes (attrs, R. styleable. roundAngleImageView); roundWidth =. getDimensionPixelSize (R. styleable. roundAngleImageView_roundWidth, roundWidth); roundHeight =. getDimensionPixelSize (R. styleable. roundAngleImageView_roundHeight, roundHeight);. recycle ();} else {float density = context. getResources (). getDisplayMetrics (). density; roundWidth = (int) (roundWidth * density); roundHeight = (int) (roundHeight * density);}/** override draw () * // @ Overridepublic void draw (Canvas canvas) {// instantiate a bitmapBitmap of the same size as ImageView. bitmap = Bitmap. createBitmap (getWidth (), getHeight (), Config. ARGB_8888); // instantiate a canvas. The memory of this canvas is bitmapCanvas canvas2 = new Canvas (bitmap); if (bitmap. isRecycled () {bitmap = Bitmap. createBitmap (getWidth (), getHeight (), Config. ARGB_8888); canvas2 = new Canvas (bitmap);} // draw the imageView to canvas2 by yourself. This causes the bitmap to store imageViewsuper. draw (canvas2); // draw a rounded rectangle using canvas, which modifies the bitmap data drawRoundAngle (canvas2); // draws the cropped bitmap to the current canvas of the system, in this way, the cropped imageview can be displayed on the screen. Paint = new paint (); Paint. setXfermode (null); canvas. drawBitmap (bitmap, 0, 0, paint); bitmap. recycle ();} public void setRoundWidth (int roundWidth, int roundHeight) {this. roundWidth = roundWidth; this. roundHeight = roundHeight;} private void drawRoundAngle (Canvas canvas) {Paint maskPaint = new Paint (); maskPaint. setAntiAlias (true); maskPaint. setXfermode (new porterduxfermode (PorterDuff. mode. CLEAR); Path maskPath = new Path (); maskPath. addRoundRect (new RectF (0.0F, 0.0F, getWidth (), getHeight (), roundWidth, roundHeight, Path. direction. CW); // This is the filling mode, which is critical to maskPath. setFillType (Path. fillType. INVERSE_WINDING); canvas. drawPath (maskPath, maskPaint );}}
How does android rewrite imageview to make the image have a rounded corner effect?
Android custom rounded corner ImageView and sawtooth Processing
When many developers want to use a rounded image during development, the solution is as follows:
1. redraw an image.
2. Configure through Layout
3. Rewrite View to implement
Here we will not talk about 1 and 2. We will focus on the implementation of method 3.
Implementation 1: draw by capturing a circular area of the Canvas and the intersection of the image. Disadvantages: obvious serrations. Set painting, Canvas anti-aliasing is invalid.
Package com. open. circleimageview. widget;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Canvas;
Import android. graphics. Paint;
Import android. graphics. PaintFlagsDrawFilter;
Import android. graphics. Path;
Import android. graphics. Rect;
Import android. graphics. Region;
Import android. util. AttributeSet;
Import android. view. View;
Public class CircleImageViewA extends View {
Public CircleImageViewA (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}
Public CircleImageViewA (Context context, AttributeSet attrs ){
Super (context, attrs );
}
Public CircleImageViewA (Context context ){
Super (context );
}
Private Bitmap bitmap;
Private Rect bitmapRect = new Rect ();
Private PaintFlagsDrawFilter pdf = new PaintFlagsDrawFilter (0, Paint. ANTI_ALIAS_FLAG | Paint. FILTER_BITMAP_FLAG );
Private Paint paint = new Paint ();
{
Paint. setStyle (Paint. Style. STROKE );
Paint. setFlags (Paint. ANTI_ALIAS_FLAG );
Paint. setAntiAlias (true); // you can call this operation to set the watermark effect. True indicates the removal, and you will understand the effect.
}
Private Path mPath = new Path ();
Public void setImageBitmap (Bitmap bitmap)
{
This. bitmap = bitmap;
}
@ Override
Protected void onDraw (Canvas canvas ){
If (null = bitmap)
{
Return;
}
BitmapRect. set (0, 0, getWidth (), getHeight ());
... The remaining full text>
How to Set a rounded corner for imageview in android
Drawable creates xml <shape> <corners>, which is available in google search.