Android 自定义圆环


定义画笔


赋值


代码:

publicclassDrawRingextends View {

    // 定义画笔private Paint mPaint1;

    private Paint mPaint2;

    private Paint mPaint3;

    private Paint mPaintText;

    @RequiresApi(api = Build.VERSION_CODES.M)

    public DrawRing(Context context, AttributeSet attrs) {

        super(context, attrs);

        mPaint1 =new Paint();

        mPaint1.setAntiAlias(true);

        mPaint1.setColor(context.getColor(R.color.ring_test1));

        // mPaint1.setColor(Color.GREEN);

        //  设置样式为,空心的,这样那园环就出来了        mPaint1.setStyle(Paint.Style.STROKE);

        // 设置那个圆环的粗细mPaint1.setStrokeWidth(40);

        mPaint2 =new Paint();

        mPaint2.setAntiAlias(true);

        mPaint2.setColor(context.getColor(R.color.ring_test2));

        // mPaint2.setColor(Color.RED);

        //  设置样式为,空心的,这样那园环就出来了        mPaint2.setStyle(Paint.Style.STROKE);

        // 设置那个圆环的粗细mPaint2.setStrokeWidth(40);

        mPaint3 =new Paint();

        mPaint3.setAntiAlias(true);

        mPaint3.setColor(context.getColor(R.color.ring_test3));

        // mPaint3.setColor(Color.BLUE);

        //  设置样式为,空心的,这样那园环就出来了        mPaint3.setStyle(Paint.Style.STROKE);

        // 设置那个圆环的粗细mPaint3.setStrokeWidth(40);

        mPaintText =new Paint();

        mPaintText.setAntiAlias(true);

        mPaintText.setColor(Color.BLACK);

        mPaintText.setTextSize(24);

    }

    privateint w;

    privateint h;

    privatefinalintMOVE_VALUE = 88;

    @Override

    protectedvoidonMeasure(intwidthMeasureSpec,int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        w = MeasureSpec.getSize(widthMeasureSpec);

        h = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(w, h);

    }

    @Override

    protectedvoid onDraw(Canvas canvas) {

        super.onDraw(canvas);

        // 定义一个区域

        // 左边,上边,都给0

        // 右边与下边都给200RectF rectF =newRectF(20, 20, 356, 356);

        /*canvas.drawArc(rectF, 0, 241.2f, false, mPaint1);

        canvas.drawArc(rectF, 241.2f, 10, false, mPaint2);*/// useCenter:false  代表不花圆心

        // canvas.drawArc(rectF, 此值是开始点, 此值并不是从开始点到此值点 它会记录之前的值自动进行累加,false, mPaint1);

        // 360 * 0.67 = 241.2canvas.drawArc(rectF, 0 - MOVE_VALUE, 241.2f,false, mPaint1);

        // 360 * 0.23canvas.drawArc(rectF, 241.2f - MOVE_VALUE, 82.0f,false, mPaint2);

        // 360 * 0.10canvas.drawArc(rectF, 241.2f + 82.0f - MOVE_VALUE, 36,false, mPaint3);

        canvas.drawText("67%", canvas.getWidth() - 70, (canvas.getHeight() / 2) + 80, mPaintText);

        canvas.drawText("23%", 5, (canvas.getHeight() / 2) - 26, mPaintText);

        canvas.drawText("10%", 116, 34, mPaintText);

      // draw();    }

    // 可以实现缓慢加载绘制的效果,但没有实际意义,就全部去除了/*private int countI;

    private void draw() {

        countI++;

        if (countI < 120) {

            SystemClock.sleep(18);

            invalidate();

        }

    }*/}

你可能感兴趣的:(Android 自定义圆环)