Package com.pan.util;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.Config;
Import Android.graphics.Canvas;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.graphics.PorterDuff;
Import Android.graphics.PorterDuffXfermode;
Import android.graphics.drawable.BitmapDrawable;
Import android.graphics.drawable.Drawable;
Import Android.util.AttributeSet;
Import Android.widget.ImageView;
/**
* Custom Circular ImageView
* @author clock to buy
* @version 1.0
* @since 2014-5-30
*/
public class Roundimageview extends ImageView {
Public Roundimageview (Context context) {
Super (context);
TODO auto-generated Constructor stub
}
Public Roundimageview (context context, AttributeSet Attrs) {
Super (context, attrs);
}
Public Roundimageview (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
}
@Override
protected void OnDraw (canvas canvas) {
Gets the drawable of the current control
drawable drawable = getdrawable ();
if (drawable = = null) {
Return
}
The width and height of the get back here is the width and height corresponding to the current control (in XML settings)
if (getwidth () = = 0 | | getheight () = = 0) {
Return
}
Brush
Paint paint = new paint ();
Color settings
Paint.setcolor (0xff424242);
Anti-aliasing
Paint.setantialias (TRUE);
The xfermode,porterduff.mode.src_in of Paint takes a two-layer image of the intersection department, showing only the upper image.
Porterduffxfermode Xfermode = new Porterduffxfermode (PorterDuff.Mode.SRC_IN);
Get bitmap, which is the bitmap of incoming ImageView
Bitmap Bitmap = ((bitmapdrawable) drawable). Getbitmap ();
Sign
int saveflags = Canvas.matrix_save_flag | Canvas.clip_save_flag | Canvas.has_alpha_layer_save_flag | Canvas.full_color_layer_save_flag | Canvas.clip_to_layer_save_flag;
Canvas.savelayer (0, 0, getwidth (), getheight (), NULL, saveflags);
Draw a mask and draw a circle that matches the size of the space
Canvas.drawcircle (GetWidth ()/2, GetHeight ()/2, getwidth ()/2, paint);
Paint.setxfermode (Xfermode);
The size of the space/bitmap the size of the =bitmap scale in multiples of
float ScaleWidth = ((float) getwidth ())/Bitmap.getwidth ();
float ScaleHeight = ((float) getheight ())/Bitmap.getheight ();
Matrix matrix = new Matrix ();
Matrix.postscale (ScaleWidth, ScaleHeight);
Bitmap Scaling
Bitmap = Bitmap.createbitmap (bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), Matrix, True);
Draw up
Canvas.drawbitmap (bitmap, 0, 0, paint);
Paint.setxfermode (NULL);
Canvas.restore ();
}
}
Custom ImageView Circular Picture