android 画圆

public class CircleImage extends View {

	private int centerY;
	private int centerX;
	private int outerRadius;
	private Paint circlePaint;
	private int defaultColor = Color.GRAY;

	public CircleImage(Context context) {
		super(context);
		init(context, null);
	}

	public CircleImage(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public CircleImage(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawCircle(centerX, centerY, outerRadius , circlePaint);
		super.onDraw(canvas);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		centerX = w / 2;
		centerY = h / 2;
		outerRadius = Math.min(w, h) / 2;
	}

	public void setColor(int color) {
		this.defaultColor = color;
		circlePaint.setColor(defaultColor);

		this.invalidate();
	}

	private void init(Context context, AttributeSet attrs) {
		//this.setScaleType(ScaleType.CENTER_INSIDE);

		circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setStyle(Paint.Style.FILL);

		int color = defaultColor;
		if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.View);
            color = a.getColor(R.styleable.View_background, color);
			a.recycle();
		}

		setColor(color);
	}

    @Override
    public void setBackgroundColor(int color) {
        setColor(color);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值