publicclassCircleImageViewextendsImageView{publicCircleImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}publicCircleImageView(Context context, AttributeSet attrs) {super(context, attrs);}publicCircleImageView(Context context) {super(context);}@OverridepublicvoidsetBackgroundDrawable(Drawable background) {super.setBackgroundDrawable(getCircleDrawable(getResources(), background));}@OverridepublicvoidsetBackgroundResource(int resid) {//Don't worry, we don't need to override it,because it will be call //setBackgroundDrawable(Drawable background)super.setBackgroundResource(resid);}@OverridepublicvoidsetImageBitmap(Bitmap bm) {//Don't worry, we don't need to override it,because it will be call //setImageDrawable(Drawable drawable)super.setImageBitmap(bm);}@OverridepublicvoidsetImageDrawable(Drawable drawable) {super.setImageDrawable(getCircleDrawable(getResources(), drawable));}@OverridepublicvoidsetImageURI(Uri uri) {//cheat it, let's change the way to implementsuper.setImageURI(uri);Drawable img = getCircleDrawable(getResources(), getDrawable());super.setImageDrawable(img);}@OverridepublicvoidsetImageResource(int resId) {//cheat it, let's change the way to implementDrawable img = getCircleDrawable(getResources(), resId);super.setImageDrawable(img);}privatestaticfinalint SPACING_LINE = 2;privatestatic Paint mCirclePaint = null;privatestatic Paint mLinePaint = null;privatestatic Paint getCirclePaint(){if(mCirclePaint == null){mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);}return mCirclePaint;}privatestatic Paint getLinePaint(){if(mLinePaint == null){mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);mLinePaint.setStyle(Style.STROKE);//You can use it to change the width of the linemLinePaint.setStrokeWidth(1);//You can use it to change the color of the linemLinePaint.setColor(Color.BLACK);}return mLinePaint;}/*** You can call this method to generate the circular bitmap, * even if you don't use this class*/publicstatic Bitmap getCircleBitmap(Bitmap src){if(src == null){returnnull;}int width = src.getWidth();int height = src.getHeight();int centerX = width / 2;int centerY = height / 2;int radius = Math.min(centerX, centerY) / 2;Bitmap result = Bitmap.createBitmap(radius * 2, radius * 2, Config.ARGB_8888);Canvas canvas = new Canvas(result);canvas.drawCircle(radius, radius, radius - SPACING_LINE, getCirclePaint());getCirclePaint().setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(src, -(centerX - radius), -(centerY - radius), getCirclePaint());//outer canvas.drawCircle(radius, radius, radius, getLinePaint());//innercanvas.drawCircle(radius, radius, radius - SPACING_LINE, getLinePaint());//resetgetCirclePaint().setXfermode(null);//recyclesrc.recycle();return result;}publicstatic Bitmap getCircleBitmap(Drawable src){if(src instanceof BitmapDrawable){return getCircleBitmap(((BitmapDrawable)src).getBitmap());}else{//now, i don't know how to do...thrownew UnsupportedException("Unsupported");}}publicstatic Bitmap getCircleBitmap(Resources res,int id){return getCircleBitmap(BitmapFactory.decodeResource(res, id));}publicstatic Drawable getCircleDrawable(Resources res, Bitmap src){returnnew BitmapDrawable(res,getCircleBitmap(src));}publicstatic Drawable getCircleDrawable(Resources res, Drawable src){returnnew BitmapDrawable(res,getCircleBitmap(src));}publicstatic Drawable getCircleDrawable(Resources res, int id) {returnnew BitmapDrawable(res, getCircleBitmap(res, id));}static class UnsupportedException extends RuntimeException{ privatestaticfinallong serialVersionUID = 1L;publicUnsupportedException(String str){super(str);}}
}