Android一个简单的圆环自定义View

一.效果图

 

public class CircleProgressView extends View {
    //外圈颜色
    private int mOuterColor = Color.GRAY;
    //内圈颜色
    private int mInterColor = Color.BLUE;
    //弧宽度
    private int mBorderWidth = 10; //10px
    private int mStepTextSize;
    private int mStepTextColor;

    private Paint mOuterPaint;
    private Paint mInnerPaint;

    private int mMaxStep = 0;
    private int mCurrentStep = 0;

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

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

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取xml里的属性值
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
        mOuterColor = array.getColor(R.styleable.CircleProgressView_outerColor, mOuterColor);
        mInterColor = array.getColor(R.styleable.CircleProgressView_innerColor, mInterColor);
        mBorderWidth = (int) array.getDimension(R.styleable.CircleProgressView_borderWidth, mBorderWidth);
        //释放资源
        array.recycle();

        //初始化外部画笔
        mOuterPaint = new Paint();
        mOuterPaint.setAntiAlias(true);
        mOuterPaint.setColor(mOuterColor);
        mOuterPaint.setStrokeWidth(mBorderWidth);//width 是弧的内外扩散的
        mOuterPaint.setStyle(Paint.Style.STROKE);//Fill画笔实心,Stroke描边
        mOuterPaint.setStrokeCap(Paint.Cap.ROUND);

        //初始化内部画笔
        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setColor(mInterColor);
        mInnerPaint.setStrokeWidth(mBorderWidth);//width 是弧的内外扩散的
        mInnerPaint.setStyle(Paint.Style.STROKE);//Fill画笔实心,Stroke描边
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //宽高不一致,取小的,保证是个正方形
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width > height ? height : width, width > height ? height : width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center = getWidth() / 2;
        int radius = getWidth() / 2 - mBorderWidth / 2;

        RectF rectF = new RectF(center-radius, center-radius, center+radius, center+radius);
        //绘制外圆弧
        canvas.drawArc(rectF, 135, 270, false, mOuterPaint);
        if (mMaxStep == 0) {
            return;
        }
        float sweepAngle = (float) mCurrentStep / mMaxStep;
        //绘制内圆弧
        canvas.drawArc(rectF, 135, sweepAngle * 270, false, mInnerPaint);
    }


    public synchronized void setStepMax(int mMaxStep) {
        this.mMaxStep = mMaxStep;
    }

    public synchronized void setCurrentStep(int mCurrentStep) {
        this.mCurrentStep = mCurrentStep;
        invalidate();
    }
}
 <com.text.ui.widget.CircleProgressView
            android:id="@+id/circle_progress"
            android:layout_width="@dimen/px_331"
            android:layout_height="@dimen/px_331"
            android:layout_marginTop="@dimen/px41"
            android:layout_marginStart="@dimen/px_102"
            app:outerColor= "@color/btn_weak_color"
            app:innerColor="@color/btn_color_1"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值