自定義實現豎著的漸變進度條

公司的一個項目需要實現血壓計的效果,我翻了網上的許多文章大多是通過圖片實現的,后來我看了看,自己研究了一下,用筆刷,畫矩形這樣也是可以做到的。這樣做的好處是明顯減少本身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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,455評論 25 708
  • 關于“經驗”,我以為它無處不在。昨天騰訊的一則新聞《90后夫妻創業賣饅頭,月入超2萬元》,看的我熱血沸騰。每...
    Maymei6閱讀 260評論 2 4
  • 在無奈的逃避與折磨中香消玉殞 《小城三月》雖然只是不太長的中篇小說,但是讀后給我的震撼是巨大的,一...
    許生說閱讀 601評論 0 2
  • 記:我們都在流浪,在流浪中感悟,痛苦和歡樂。所有的日子都將會過去,而這留下來的將會是永恒。 (0) 時間和非時間里...
    chuanyu2015閱讀 276評論 0 0
  • 《檢察官外傳》,主線簡單,沒有什么出乎意料的東西,很容易猜中情節,整部電影說的就是好人被陷害,然后自證清白,使得壞...
    IMBP機閱讀 312評論 0 0