双向进度条

public class RangeProgressBar extends View {
    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Paint mPaint1 = new Paint();


    float firstValue = 50;
    float maxValue = 100;
    float minValue = 0;

    float secondValue = 75;
    boolean isTwoProgress = true;


    int progressColor = getResources().getColor(R.color.progress_bg);
    //    int bg = getResources().getColor(R.color.detail);
    int bgColor = getResources().getColor(R.color.orgen);
    int controlColor = getResources().getColor(R.color.orgen);

    int controlRadius;

    int leftRightSpace;

    int topBottomSpace;
    private Paint painttxt;
    private Bitmap bitmap;
    Context context;
    private float maxTemperature;
    private float minTemperature;

    public RangeProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        invalidate();
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
        invalidate();
    }

    float len;

    private void initViews() {
        dip5 = dip2px(getContext(), 4);
        leftRightSpace = dip5 * 11;
        topBottomSpace = dip5;
//        len = getHeight() - 2 * topBottomSpace;
        len = getHeight();
//        mPaint.setStyle(Paint.Style.FILL);
//        mPaint1.setStyle(Paint.Style.FILL);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.thum);
//        controlRadius = dip5 * 2;
        painttxt = new Paint(Paint.DITHER_FLAG);
        // mPaint.setAntiAlias(true);
        // mPaint.setDither(true);
        painttxt.setColor(Color.WHITE);// 设置画笔为蓝色
        painttxt.setStrokeWidth(1.5f);
        painttxt.setStrokeJoin(Paint.Join.ROUND);
        painttxt.setStrokeCap(Paint.Cap.ROUND);
        painttxt.setTextSize(24);

        LogUtils.i("toolBarModel:"+toolBarModel);
        if (toolBarModel == 1) {
           setCountProgressHead(false, getResources().getColor(R.color.progress_bg), getResources().getColor(R.color.orgen));
        } else if (toolBarModel == 2) {
           setCountProgressBottom(false, getResources().getColor(R.color.orgen), getResources().getColor(R.color.progress_bg));
        } else if (toolBarModel == 3) {
            setCountProgressCenter(true, getResources().getColor(R.color.progress_bg), getResources().getColor(R.color.orgen));
        } else if (toolBarModel == 4) {
           setCountProgressTwo(true, getResources().getColor(R.color.orgen), getResources().getColor(R.color.progress_bg));
        }
    }

    DecimalFormat df = new DecimalFormat("0.0");

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    public void exchangeProgressBar() {
        isTwoProgress = !isTwoProgress;
        invalidate();
    }

    RectF rectf = new RectF();

    private float getRate(float value) {
        return value / maxValue;
    }

    public void setCountProgress(boolean isTwoProgress, int bgColor, int progressColor) {
        this.isTwoProgress = isTwoProgress;
        this.bgColor = bgColor;
        this.progressColor = progressColor;

        mPaint1.setColor(progressColor);
        invalidate();
    }




    private int getTextWidth(Paint paint, String text) {
        int iRet = 0;
        if (text != null && text.length() > 0) {
            int len = text.length();
            float[] widths = new float[len];
            paint.getTextWidths(text, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }

    public float var1;
    public float var2;

    @Override
    protected void onDraw(Canvas canvas) {
//        canvas.rotate(-90);
//        canvas.translate(-getHeight(), 0);
        if (getWidth() == 0) {
            return;
        }

        float heighttext = (painttxt.getFontMetrics().bottom - painttxt.getFontMetrics().top) / 2 - 2;
        //画背景
        //   mPaint.setColor(bgColor);
        rectf.left = leftRightSpace;
        rectf.top = topBottomSpace;
        rectf.right = getWidth() - controlRadius / 2;
        rectf.bottom = len;
        canvas.drawRoundRect(rectf, dip5, dip5, mPaint1);

        //画进度条
        //单进度条
        if (isTwoProgress == false) {
            fistPointX = getValueLen(firstValue);
            rectf.bottom = fistPointX;


            canvas.drawRoundRect(rectf, dip5, dip5, mPaint);

//            canvas.drawCircle(getWidth() / 2f + controlRadius / 2, fistPointX, controlRadius, mPaint);
            canvas.drawBitmap(bitmap, 0, fistPointX - DensityUtil.dp2px(context, 10), painttxt);

//            canvas.drawText(df.format(firstValue), bitmap.getWidth()/2-getTextWidth(painttxt,df.format(firstValue))/2-2, fistPointX+heighttext, painttxt);
            var1=maxTemperature - ((maxTemperature - minTemperature) * firstValue / 100.0f);
            canvas.drawText(df.format(var1), bitmap.getWidth() / 2 - getTextWidth(painttxt, df.format(firstValue)) / 2 - 2, fistPointX + heighttext, painttxt);
        } else {//双进度条

            fistPointX = getValueLen(firstValue);
            secondPointX = getValueLen(secondValue);
            rectf.top = fistPointX;
            rectf.bottom = secondPointX;

            //   mPaint.setColor(progressColor);
            canvas.drawRoundRect(rectf, dip5, dip5, mPaint);

            //     mPaint.setColor(controlColor);

//            canvas.drawCircle(fistPointX, getWidth() / 2, controlRadius, mPaint);
//            canvas.drawCircle(secondPointX, getWidth() / 2, controlRadius, mPaint);
//            canvas.drawCircle(getWidth() / 2 + controlRadius / 2, fistPointX, controlRadius, mPaint);
//            canvas.drawCircle(getWidth() / 2 + controlRadius / 2, secondPointX, controlRadius, mPaint);
            canvas.drawBitmap(bitmap, 0, fistPointX - DensityUtil.dp2px(context, 10), painttxt);
            canvas.drawBitmap(bitmap, 0, secondPointX - DensityUtil.dp2px(context, 10), painttxt);

//            LogUtils.i("maxTemperature:" + maxTemperature + "minTemperature:" + minTemperature + "firstValue:" + firstValue + "secondValue:" + secondValue);
            var1=maxTemperature - ((maxTemperature - minTemperature) * firstValue / 100.0f);
            var2=maxTemperature - ((maxTemperature - minTemperature) * secondValue / 100.0f);
            canvas.drawText(df.format(var1), bitmap.getWidth() / 2 - getTextWidth(painttxt, df.format(firstValue)) / 2 - 2, fistPointX + heighttext, painttxt);

            canvas.drawText(df.format(var2), bitmap.getWidth() / 2 - getTextWidth(painttxt, df.format(firstValue)) / 2 - 2, secondPointX + heighttext, painttxt);
        }
    }

    private float getValueLen(float value) {
        float rate = getRate(value);
        float positionX = topBottomSpace + len * rate;
        return positionX;
    }

    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    int dip5;

    float fistPointX, secondPointX;


    private boolean isInArea(float lastPosition, float x) {
        return x < lastPosition + controlRadius + bitmap.getHeight() && lastPosition - controlRadius - bitmap.getHeight() < x;
    }

    boolean isMovedFirst = false;
    boolean isMovedSecond = false;

    public void setFirstValue(float firstValue) {
        this.firstValue = firstValue;
    }

    public void setSecondValue(float secondValue) {
        this.secondValue = secondValue;
    }


    public void setTemperatureSampler(TemperatureSampler temperatureSampler) {
        maxTemperature = temperatureSampler.getTemperatureOfGlobal().maxTemperature;
        minTemperature = temperatureSampler.getTemperatureOfGlobal().minTemperature;

    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                if (isInArea(fistPointX, event.getY())) {
                    isMovedFirst = true;
                }
                if (isInArea(secondPointX, event.getY())) {
                    isMovedSecond = true;
                }

                break;
            case MotionEvent.ACTION_MOVE:
                float newValue = maxValue * x2Rate(event.getY());
                if (isMovedFirst) {
                    if (isTwoProgress) {
                        if (newValue + 10 < secondValue) {
                            if (ondataChanged != null) {
                                ondataChanged.onFirstDataChange(var1);
                            }
                            setFirstValue(newValue);
                            invalidate();
                        }
                    } else {
                        if (ondataChanged != null) {
                            ondataChanged.onFirstDataChange(var1);
                        }
                        setFirstValue(newValue);
                        invalidate();
                    }
                } else if (isMovedSecond) {
                    if (newValue > firstValue + 10) {
                        if (ondataChanged != null) {
                            ondataChanged.onSecondDataChange(var2);
                        }
                        setSecondValue(newValue);
                        invalidate();
                    }
                }
                break;
            default:
                isMovedSecond = false;
                isMovedFirst = false;
                break;
        }
        return true;
    }

    private float x2Rate(float x) {
        float f = (x + topBottomSpace) / len;
        if (f < 0) {
            f = 0;
        }
        if (f > 1) {
            f = 1;
        }
        return f;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initViews();
    }


    OnDataChanged ondataChanged;

    public void setOndataChanged(OnDataChanged ondataChanged) {
        this.ondataChanged = ondataChanged;
    }

    public interface OnDataChanged {

        void onFirstDataChange(float var);

        void onSecondDataChange(float var);
    }

    //        LinearGradient linearGradient = new LinearGradient(0,0,getMeasuredWidth(),0,colors,position, Shader.TileMode.CLAMP);

    public void setCountProgressHead(boolean isTwoProgress, int bgColor, int progressColor) {

        int[] colors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE};
        float[] position = {0f, 0.35f, 0.7f, 1.0f};
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint1 = new Paint();
        this.isTwoProgress = isTwoProgress;
        this.bgColor = bgColor;
        this.progressColor = progressColor;
        LinearGradient linearGradient = new LinearGradient(0, 0, 0, getMeasuredHeight(), colors, position, Shader.TileMode.CLAMP);
        mPaint1.setShader(linearGradient);
        mPaint.setColor(progressColor);
        invalidate();
    }

    public void setCountProgressCenter(boolean isTwoProgress, int bgColor, int progressColor) {

        int[] colors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE};
        float[] position = {0f, 0.35f, 0.7f, 1.0f};
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint1 = new Paint();
        this.isTwoProgress = isTwoProgress;
        this.bgColor = bgColor;
        this.progressColor = progressColor;
        LinearGradient linearGradient = new LinearGradient(0, 0, 0, getMeasuredHeight(), colors, position, Shader.TileMode.CLAMP);
        mPaint1.setShader(linearGradient);
        mPaint.setColor(progressColor);
        invalidate();
    }

    public void setCountProgressTwo(boolean isTwoProgress, int bgColor, int progressColor) {

        int[] colors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE};
        float[] position = {0f, 0.35f, 0.7f, 1.0f};
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint1 = new Paint();
        this.isTwoProgress = isTwoProgress;
        this.bgColor = bgColor;
        this.progressColor = progressColor;
        LinearGradient linearGradient = new LinearGradient(0, 0, 0, getMeasuredHeight(), colors, position, Shader.TileMode.CLAMP);
        mPaint.setShader(linearGradient);
        mPaint1.setColor(bgColor);
        invalidate();
    }
    public void setCountProgressBottom(boolean isTwoProgress, int bgColor, int progressColor) {

        int[] colors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE};
        float[] position = {0f, 0.35f, 0.7f, 1.0f};
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint1 = new Paint();
        this.isTwoProgress = isTwoProgress;
        this.bgColor = bgColor;
        this.progressColor = progressColor;
        LinearGradient linearGradient = new LinearGradient(0, 0, 0, getMeasuredHeight(), colors, position, Shader.TileMode.CLAMP);
        mPaint1.setColor(bgColor);
        mPaint.setShader(linearGradient);
        invalidate();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值