android 多状态百分比进度条(手动设置百分比)

只是记录下学习的过程哈
刚开始研究这个东西,这个也是之前项目里遇到的一个 ,当时百度好久都没找到,现在自己返回来写了写 还有好多小问题,慢慢处理


/**
 * author : 桶哥二号
 * motto : Anything is possible
 * date   : 2019-12-1211:23
 * desc   : 我好难呀,我太难了呀
 * version: 1.0
 */
public class CustomProgressBar extends View {
    private int startColor;//起始颜色
    private int modleColor;//中间颜色
    private int endColor;//末尾颜色
    private float startPercent;//起始百分比
    private float modlePercent;//中间百分比
    private float startPercen;//起始百分比
    private float modlePercen;//中间百分比
    private float endPercent;//末尾百分比
    private float progressWidth;//进度条的宽度

    private int defaultWidth;
    private int defaultHeight;
    private int width;//计算出的宽度
    private int height;//计算出的高度
    private int paddingLeft = getPaddingLeft();
    private Paint paintS = null;
    private Paint paintM = null;
    private Paint paintE = null;


    int textColor;

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

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);

    }

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

    private void init(Context context, AttributeSet attrs) {
        getScreenWidth(context);

        Log.i("屏幕的宽度为:", "--------" + getScreenWidth(context));
        defaultWidth = DpOrPxUtils.dip2px(context, (getScreenWidth(context) - 200) / 2);
        defaultHeight = DpOrPxUtils.dip2px(context, 30);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomLines);
        startColor = ta.getColor(R.styleable.CustomLines_startColor, Color.RED);
        modleColor = ta.getColor(R.styleable.CustomLines_modleColor, Color.GREEN);
        endColor = ta.getColor(R.styleable.CustomLines_endColor, Color.YELLOW);
        TypedArray taPercent = context.obtainStyledAttributes(attrs, R.styleable.CustomPercent);
        startPercent = taPercent.getFloat(R.styleable.CustomPercent_startPercent, 300);
        modlePercent = taPercent.getFloat(R.styleable.CustomPercent_startPercent, 500);
        endPercent = taPercent.getFloat(R.styleable.CustomPercent_endPercent, 600);
        progressWidth = taPercent.getFloat(R.styleable.CustomPercent_progressWidth, 20);
        paintS = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintS.setStyle(Paint.Style.FILL);
        paintS.setStrokeWidth(progressWidth);
        paintS.setStrokeCap(Paint.Cap.ROUND);
        paintS.setColor(startColor);
        paintM = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintM.setStyle(Paint.Style.FILL);
        paintM.setStrokeWidth(progressWidth);
        paintM.setColor(modleColor);
        paintE = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintE.setStyle(Paint.Style.FILL);
        paintE.setStrokeWidth(progressWidth);
        paintE.setStrokeCap(Paint.Cap.ROUND);
        paintE.setColor(endColor);
        ta.recycle();
        taPercent.recycle();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getWidSize(widthMeasureSpec);
        height = getHeiSize(heightMeasureSpec);
        setMeasuredDimension(width, height);

    }

    private int getWidSize(int measureSpace) {
        int result;
        int mode = MeasureSpec.getMode(measureSpace);
        int size = MeasureSpec.getSize(measureSpace);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else if (mode == MeasureSpec.AT_MOST) {

            result = Math.min(defaultWidth + defaultHeight, size);
        } else {
            result = defaultWidth;
        }
        return result;
    }

    private int getHeiSize(int measureSpace) {
        int result;
        int mode = MeasureSpec.getMode(measureSpace);
        int size = MeasureSpec.getSize(measureSpace);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            result = Math.min(defaultHeight , size);
        } else {
            result = defaultHeight;
        }
        return result;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i("左边的距离", getPaddingLeft() + "");

        startPercent = startPercen * ((width-height ) / 100);
        modlePercent = modlePercen * ((width-height ) / 100);
        Log.i("计算后的百分比", startPercent+"============"+modlePercent);
        canvas.drawLine(height/2, 50, startPercent + height/2, 50, paintS);
        canvas.drawLine(startPercent + height + modlePercent, 50, width-height, 50, paintE);
        canvas.drawLine(startPercent + height/2, 50, startPercent + height + modlePercent, 50, paintM);

    }

    public void setPerCent(String startPercent, String modlePercent) {
        this.startPercen = Float.valueOf(startPercent);
        this.modlePercen = Float.valueOf(modlePercent);

    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值