默认progress

效果图,颜色可以在几改,个人感觉效果不好看



代码如下:

package com.example.mengsong.demo01;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by MengSong on 2016/12/20.
 */
public class CustomProgress extends View {

    private Paint mPaint;

    private RectF mRectF;

    private float startAngle = 0;

    private float sweepAngle = 20;

    private long heightBrightPosition = 0;

    public CustomProgress(Context context) {
        super(context);
    }

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

    /**
     * 没20度画一个扇形,中间空10度的区域
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < 12; i++) {
            if (i == heightBrightPosition % 12){
                //滚动扇形的颜色
                mPaint.setColor(Color.RED);
            }else {
                mPaint.setColor(Color.GREEN);
            }
            canvas.save();
            canvas.drawArc(mRectF, startAngle, sweepAngle, false, mPaint);
            canvas.restore();
            startAngle += 30;
        }
        heightBrightPosition++;
        postInvalidateDelayed(60);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int stroke = getMeasuredWidth() / 2 - 10;
        mRectF = new RectF(stroke / 2, stroke / 2, getMeasuredWidth() - stroke / 2, getMeasuredHeight() - stroke / 2);
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(stroke - 15);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.GREEN);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measuredWidth = measureWidth(widthMeasureSpec);
        int measuredHeight = measureHeight(heightMeasureSpec);
        if (measuredHeight != measuredHeight) {
            int min = Math.min(measuredWidth, measuredHeight);
            setMeasuredDimension(min, min);
        } else {
            setMeasuredDimension(measuredWidth, measuredHeight);
        }
    }

    /**
     * 计算控件宽度
     *
     * @param widthMeasureSpec
     * @return
     */
    private int measureWidth(int widthMeasureSpec) {
        int result = 0;
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);

        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = 200;
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;
    }

    /**
     * 计算控件高度
     *
     * @param heightMeasureSpec
     * @return
     */
    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);

        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = 200;
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值