方案一:继承自ImageView
package com.chy.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CircleImageView extends ImageView {
private Paint paint = null;
private PaintFlagsDrawFilter pfdf = null;
private Path path = null;
public CircleImageView(Context context) {
super(context);
init(context, null);
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
int clearBits = 0;
int setBits = Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG;
pfdf = new PaintFlagsDrawFilter(clearBits, setBits);
setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
}
@Override
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
if (path == null) {
path = new Path();
path.addCircle(width / 2f, height / 2f, Math.min(width / 2f, height / 2f), Path.Direction.CCW);
path.close();
}
int saveCount = canvas.save();
canvas.setDrawFilter(pfdf);
canvas.clipPath(path, Region.Op.INTERSECT);
super.onDraw(canvas);
canvas.restoreToCount(saveCount);
}
}
方案二:继承自View
package com.chy.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView.ScaleType;
public class CircleView extends View {
private Paint paint = null;
private PaintFlagsDrawFilter pfdf = null;
private PorterDuffXfermode xfermode = null;
private RectF rectf = null;
private Bitmap bitmap = null;
private Bitmap destBmp = null;
private Matrix matrix = null;
private ScaleType scaleType = ScaleType.CENTER_CROP;
public CircleView(Context context) {
super(context);
init(context, null);
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
try {
if (android.os.Build.VERSION.SDK_INT >= 11) {
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
} catch (Exception e) {
e.printStackTrace();
}
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
int clearBits = 0;
int setBits = Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG;
pfdf = new PaintFlagsDrawFilter(clearBits, setBits);
xfermode = new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
matrix = new Matrix();
}
public void setImageResource(int resId) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resId);
setBitmap(bmp);
}
public void setBitmap(Bitmap bmp) {
this.bitmap = bmp;
}
public void setScaleType(ScaleType scaleType) {
this.scaleType = scaleType;
}
private void makeDestBmp(int width, int height) {
destBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
rectf = new RectF(0, 0, width, height);
Canvas canvas = new Canvas(destBmp);
canvas.drawOval(rectf, paint);
}
@Override
protected void onDraw(Canvas canvas) {
if (bitmap == null) {
return;
}
int width = getWidth();
int height = getHeight();
int bmpWidth = bitmap.getWidth();
int bmpHeight = bitmap.getHeight();
if (destBmp == null) {
makeDestBmp(width, height);
}
canvas.setDrawFilter(pfdf);
canvas.drawBitmap(destBmp, 0, 0, paint);
paint.setXfermode(xfermode);
switch(scaleType) {
case FIT_XY:
canvas.drawBitmap(bitmap, null, rectf, paint);
break;
case CENTER_CROP:
default:
matrix.reset();
float scale = Math.max((float) width / (float) bmpWidth, (float) height / (float) bmpHeight);
matrix.postScale(scale, scale, 0, 0);
matrix.postTranslate((width - bmpWidth * scale) / 2f, (height - bmpHeight * scale) / 2f);
canvas.drawBitmap(bitmap, matrix, paint);
break;
}
paint.setXfermode(null);
}
}