Custom ImageView Circular Picture

Source: Internet
Author: User

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

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.