搜索内容

有一个问题?

如果您有任何疑问,可以在下面询问或输入您要寻找的!

Android 自定义View 之 Path PathMeasure (三)之渐变色进度条Progr

生成海报
LeeStudio_
LeeStudio_ 2020-09-21 19:08
阅读需:0

可调节渐变色的时间轴Progress

在这里插入图片描述
如圖所显示

要求情景:

  • 时间轴
  • 渐变色
  • 可静态数据\可全自动

编码分析

  • 复位情况时间轴画笔工具、市场前景可渐变色时间轴画笔工具
	/**
	复位情况时间轴画笔工具、市场前景可渐变色时间轴画笔工具
	**/
    private void initView(Context context, AttributeSet attrs) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressCirce);
        gradientLeftColor = typedArray.getColor(R.styleable.ProgressCirce_gradient_left_color, -1);
        gradientRightColor = typedArray.getColor(R.styleable.ProgressCirce_gradient_right_color, -1);
        normalColor = typedArray.getColor(R.styleable.ProgressCirce_gradient_normal_color, 0xFFF4F4F6);


        backPaint = new Paint();
        backPaint.setStyle(Paint.Style.FILL);
        backPaint.setColor(normalColor);
        backPaint.setAntiAlias(true);


        forPaint = new Paint();
        forPaint.setAntiAlias(true);
        forPaint.setStyle(Paint.Style.FILL);

        typedArray.recycle();

//        forPaint.setColor(Color.RED);
    }

  • 应用path复位情况进展途径
   @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        forPath = new Path();
        backPath = new Path();
        backPath.addRoundRect(
                getPaddingLeft(),
                getPaddingTop(),
                getMeasuredWidth(),
                getMeasuredHeight(),
                radius, radius, Path.Direction.CCW);


    }
  • 绘图
    根据传到当今的progress 和progressMax(最高值)测算出当今 进展处在width 中什么位置
    获得终止部位stopD ,绘图出市场前景progress
   forPath.reset();
        canvas.drawPath(backPath, backPaint);

        float stopD = (float) (Math.abs(progress)) / progressMax * getMeasuredWidth();
        forPaint.setShader(getLinearGradient());
        forPath.addRoundRect(
                getPaddingLeft(),
                getPaddingTop(),
                stopD,
                getMeasuredHeight(),
                radius, radius, Path.Direction.CCW);
        if (progress != 1) {
            canvas.drawPath(forPath, forPaint);
        }
  • 线形渐变色
    获得线形渐变色值,通告传到的color id,设定渐变色。

    /**
     * 获得线形渐变色
     *
     * @return
     */
    private LinearGradient getLinearGradient() {
        if (linearGradient == null) {
            linearGradient = new LinearGradient(0, 0,
                    getMeasuredWidth(),
                    getMeasuredHeight(),
                    gradientLeftColor,
                    gradientRightColor,
                    Shader.TileMode.CLAMP); //依据R文档中的id获得到color
        }
        return linearGradient;
    }

  • 打开动态性progress动漫
  /**
     * 打开全自动进展动漫
     */
    public void startAutoProcessAnimation(final AnimationInterface animationInterface) {
        if (valueAnimator == null) {
            valueAnimator = ValueAnimator.ofInt(0, (int) progressMax);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {


                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    setProgress((Integer) animation.getAnimatedValue());
                    if (animationInterface != null) {
                        animationInterface.animationRunning((Integer) animation.getAnimatedValue());
                    }
                }

            });
            valueAnimator.setDuration(2000);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.start();
        }
    }

    /**
     * 终止全自动进展动漫
     */
    public void stopAutoProcessAnimation() {
        if (valueAnimator != null) {
            valueAnimator.cancel();
            valueAnimator = null;
        }
    }

    public interface AnimationInterface {
        void animationRunning(int progress);

    }
  • 设定静态数据progress
  /**
     * 设定静态数据progress
     *
     * @param progress
     */
    public void setProgress(int progress) {
        if (progress > progressMax) {
            this.progress = (int) progressMax;
        } else if (progress <= 0) {
            this.progress = 1;
        } else {
            this.progress = progress;
        }
        invalidate();
    }

到此,可渐变色的Progress就完成了。

评论
  • 消灭零回复