尝试这个:

public class TransparentCircle extends View {

Bitmap bm;

Canvas cv;

Paint eraser;

public TransparentCircle(Context context) {

super(context);

Init();

}

public TransparentCircle(Context context, AttributeSet attrs) {

super(context, attrs);

Init();

}

public TransparentCircle(Context context, AttributeSet attrs,

int defStyleAttr) {

super(context, attrs, defStyleAttr);

Init();

}

private void Init(){

eraser = new Paint();

eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

eraser.setAntiAlias(true);

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

if (w != oldw || h != oldh) {

bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

cv = new Canvas(bm);

}

super.onSizeChanged(w, h, oldw, oldh);

}

@Override

protected void onDraw(Canvas canvas) {

int w = getWidth();

int h = getHeight();

int radius = w > h ? h / 2 : w / 2;

bm.eraseColor(Color.TRANSPARENT);

cv.drawColor(Color.BLUE);

cv.drawCircle(w / 2, h / 2, radius, eraser);

canvas.drawBitmap(bm, 0, 0, null);

super.onDraw(canvas);

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐