自定义实现竖着的渐变进度条

公司的一个项目需要实现血压计的效果,我翻了网上的许多文章大多是通过图片实现的,后来我看了看,自己研究了一下,用笔刷,画矩形这样也是可以做到的。这样做的好处是明显减少本身app的大小,降低各模块之间的耦合度。
具体思路:

1.进度条,其实就是一个最大值和最小值的比例值;

2.drawRoundRect 画圆角矩形

3.LinearGradient对象渲染,具体渲染的比例要自己计算,目前我的demo提供3中颜色渲染,就是超过三分之一时是另外一种颜色。

关键代码:

public class ProgressView extends View {

    private static final int[] SECTION_COLORS = {Color.RED, Color.YELLOW, Color.GREEN};

    private float maxCount;

    private float currentCount;

    private Paint mPaint;

    private int mWidth, mHeight;



    public ProgressView(Context context, AttributeSet attrs,

                        int defStyleAttr) {

        super(context, attrs, defStyleAttr);

    }



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



    public ProgressView(Context context) {

        super(context);

    }

    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        mPaint = new Paint();

        mPaint.setAntiAlias(true);

        int round = mHeight / 2;

        mPaint.setColor(Color.rgb(71, 76, 80));

        RectF rectBg = new RectF(0, 0, mWidth, mHeight);

        canvas.drawRoundRect(rectBg, round, round, mPaint);

        mPaint.setColor(Color.GRAY);

        RectF rectBlackBg = new RectF(2, 2, mWidth - 2, mHeight - 2);

        canvas.drawRoundRect(rectBlackBg, round, round, mPaint);

        float section = currentCount / maxCount;

        RectF rectProgressBg = new RectF(3, (mHeight - 3) * (1.0f - section), mWidth - 3, mHeight - 3);

        if (section <= 1.0f / 3.0f) {

            if (section != 0.0f) {
                mPaint.setColor(SECTION_COLORS[2]);
            } else {
                mPaint.setColor(Color.TRANSPARENT);
            }

        } else {

            int count = (section <= 1.0f / 3.0f * 2) ? 2 : 3;

            int[] colors = new int[count];

            float[] positions = new float[count];

            if (count == 2) {

                positions[0] = 0.0f;

                positions[1] = 1.0f - positions[0];

                //因为是竖着的进度条,所以需要从原数组的第二个位置开始

                System.arraycopy(SECTION_COLORS, 1, colors, 0, count);



            } else {

                positions[0] = 0.0f;

                positions[1] = (maxCount / 3) / currentCount;

                positions[2] = 1.0f - positions[0] * 2;

                System.arraycopy(SECTION_COLORS, 0, colors, 0, count);

            }

            positions[positions.length - 1] = 1.0f;



            LinearGradient shader = new LinearGradient(3, (mHeight - 3) * (1.0f - section), mWidth - 3, mHeight - 3, colors, null, Shader.TileMode.MIRROR);

            mPaint.setShader(shader);

        }

        canvas.drawRoundRect(rectProgressBg, round, round, mPaint);

    }



    private int dipToPx(int dip) {

        float scale = getContext().getResources().getDisplayMetrics().density;

        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));

    }
    public void setMaxCount(float maxCount) {
        this.maxCount = maxCount;
    }
    public void setCurrentCount(float currentCount) {

        this.currentCount = currentCount > maxCount ? maxCount : currentCount;
        invalidate();

    }



    public float getMaxCount() {

        return maxCount;

    }



    public float getCurrentCount() {

        return currentCount;

    }



    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);

        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {

            mWidth = widthSpecSize;

        } else {

            mWidth = 0;

        }

        if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {

            mHeight = dipToPx(15);

        } else {

            mHeight = heightSpecSize;

        }

        setMeasuredDimension(mWidth, mHeight);

    }

}

demo下载
具体效果如图:

QQ图片20170505161909.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,188评论 25 708
  • 关于“经验”,我以为它无处不在。昨天腾讯的一则新闻《90后夫妻创业卖馒头,月入超2万元》,看的我热血沸腾。每...
    Maymei6阅读 293评论 2 4
  • 在无奈的逃避与折磨中香消玉殒 《小城三月》虽然只是不太长的中篇小说,但是读后给我的震撼是巨大的,一...
    许生说阅读 632评论 0 2
  • 记:我们都在流浪,在流浪中感悟,痛苦和欢乐。所有的日子都将会过去,而这留下来的将会是永恒。 (0) 时间和非时间里...
    chuanyu2015阅读 351评论 0 0
  • 《检察官外传》,主线简单,没有什么出乎意料的东西,很容易猜中情节,整部电影说的就是好人被陷害,然后自证清白,使得坏...
    IMBP机阅读 341评论 0 0

友情链接更多精彩内容