Android-画圆环

绘制圆弧方法

public void drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

参数说明

oval:圆弧所在的椭圆对象。

startAngle:圆弧的起始角度。

sweepAngle:圆弧的角度。

useCenter:是否显示半径连线,true表示显示圆弧与圆心的半径连线,false表示不显示。

paint:绘制时所使用的画笔。

 

public class CircleView extends View {
//    public CircleView(Context context) {
//        super(context);
//    }
//
//    public CircleView(Context context, @Nullable AttributeSet attrs) {
//        super(context, attrs);
//    }
//
//    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//        init(context);
//        getAttrs(attrs);
//    }
//
//    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//    }

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
        getAttrs(attrs);
    }

    private Context mContext;
    private int mBackGroundColor, mForegroundColor;
    private int mWidth, mHeight;
    private int mStrokeWidth;
    private Paint mProgressPaint;
    private CircleModel model = CircleModel.MODE_NONE;
    private float mSweepAngle; //角度
    private float mStartAngle = 90;//开始角度

    private void init(Context context) {
        mContext = context;
        mStrokeWidth = dp2px(8);
        mProgressPaint = getPaint();
    }

//    private void getAttrs(AttributeSet attrs) {
//        if (attrs == null) {
//            return;
//        }
//        @SuppressLint({"CustomViewStyleable", "Recycle"}) TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
//        mBackGroundColor = array.getColor(R.styleable.CircleProgressView_num_progress_background, Color.parseColor("#ffffff"));
//        mForegroundColor = array.getColor(R.styleable.CircleProgressView_num_progress_foreground, Color.parseColor("#4785FF"));
//        array.recycle();
//    }

    private void getAttrs(AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        @SuppressLint("CustomViewStyleable") TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.CircleView);
        if (array == null) {
            return;
        }
        mBackGroundColor = array.getColor(R.styleable.CircleView_num_progress_background, Color.parseColor("#ffffff"));
        mForegroundColor = array.getColor(R.styleable.CircleView_num_progress_foreground, Color.parseColor("#4785FF"));
        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    /**
     * 获取画笔
     */
    private Paint getPaint() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(mStrokeWidth);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        return paint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (model == CircleModel.MODEL_DETAIL) {
            //绘制背景
            int cx = mWidth / 2;
            int cy = mHeight / 2;
            int radius = mWidth / 2 - mStrokeWidth;
            mProgressPaint.setShader(null);
            mProgressPaint.setColor(mBackGroundColor);
            canvas.drawCircle(cx, cy, radius, mProgressPaint);

            //绘制前景 进度
            int left = mWidth / 2 - radius;
            int top = mHeight / 2 - radius;
            int right = mWidth - mStrokeWidth;
            int bottom = mHeight - mStrokeWidth;
            @SuppressLint("DrawAllocation") RectF oval = new RectF(left, top, right, bottom);
            mProgressPaint.setColor(mForegroundColor);
            canvas.drawArc(oval, -180, 90, false, mProgressPaint);
            return;
        }
    }

    /**
     * 设置画图的类型
     * @param model
     */
    public void setModel(CircleModel model) {
        this.model = model;
    }

    /**
     * 设置分数
     */
    public void setScore(int score) {
        mSweepAngle = 180 * score / 100f;
        postInvalidate();
    }

    public enum CircleModel {
        MODEL_DETAIL,
        MODEL_SINGLE,
        MODEL_RING,
        MODE_NONE
    }

    /**
     * dp转px
     */
    private int dp2px(float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值