效果图:
页面代码:
public class CircleView extends ImageView {
private Paint mPaint = new Paint();
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable mDrawable = getDrawable();
if (null == mDrawable) super.onDraw(canvas);
Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
int len = getWidth() < getHeight() ? getWidth() : getHeight();
Bitmap tempBitmap = Bitmap.createBitmap(len, len, Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(tempBitmap);
mPaint.setAntiAlias(true);
mCanvas.drawCircle(len / 2, len / 2, len / 2, mPaint);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
Matrix matrix = new Matrix();
matrix.setScale((float) len / mBitmap.getWidth(), (float) len / mBitmap.getHeight());
mCanvas.drawBitmap(mBitmap, matrix, mPaint);
mPaint.reset();
canvas.drawBitmap(tempBitmap, 0, 0, mPaint);
}
}