Android實現(xiàn)帶有指示器的進度條
背景
當我們看到UI給我們設計的效果的時候,我們習慣性的思路就是看看google有沒有為我們提供相應的控件或者是能否在網(wǎng)上找到一些合適的輪子拿過來直接用。但是,有時候很不巧的是沒有這樣的輪子供我們直接使用,而且,UI以及產(chǎn)品就需要這樣的效果的時候,我們就只能自己進行實現(xiàn)繪制。今天呢,我就帶大家從簡單的實現(xiàn)一個帶有指示器的進度條,幫大家理解一下之定義View的繪制流程。
效果實現(xiàn)圖
俗話說,沒有效果圖,就等于是耍那個啥,所以按照慣例,貼圖:
開干
當我們決定要自己進行繪制的時候,我們首先需要對需求,效果進行觀察、分析、拆解。就是所謂的大事化小,小事化無。
- 觀察:效果圖中包含 進度條、帶三角箭頭的圓角指示器、進度值以及進度動畫
- 分析:通過觀察我們可以將整個進度條分為 指示器,進度條、進度值、動畫4部分
- 拆解:因此我們可以依次對上述部分的組成部分進行單獨的繪制最后整體關聯(lián)是實現(xiàn)上圖的效果
自定義View的流程(按照上述部分進行)
大家應該都知道繪制流程包含三部分,自定義屬性、測量 measure、draw、layout。但是,我們今天所講的是進度條View,而不是ViewLayout,所以layout的部分今天暫且不講。
自定義屬性以及代碼中獲取
屬性 | 說明 |
---|---|
app:hpb_centerPadding="5dp" | 指示器與進度條的間距 |
app:hpb_progressBarBackgroundColor="@color/colorPrimary" | 進度條背景色 |
app:hpb_progressBarForegroundColor="@color/colorAccent" | 進度條前景色 |
app:hpb_progressBarHeight="2dp" | 指示器bottomPadding |
app:hpb_textBottomPadding="5dp" | 指示器與進度條的間距 |
app:hpb_textLeftPadding="5dp" | 指示器leftPadding |
app:hpb_textRightPadding="5dp" | 指示器rightPadding |
app:hpb_textTopPadding="5dp" | 指示器topPadding |
app:hpb_textSize="12sp" | 指示器文字大小 |
app:hpb_textColor="#FFFFFF" | 指示器文字顏色 |
app:hpb_progress="20" | 進度值 |
//獲取自定義屬性 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBar); mTextSize = ta.getDimension(R.styleable.HorizontalProgressBar_hpb_textSize, Utils.sp2px(context, 12)); mTextColor = ta.getColor(R.styleable.HorizontalProgressBar_hpb_textColor, Color.parseColor("#FFFFFF")); currentProgress = ta.getFloat(R.styleable.HorizontalProgressBar_hpb_progress, 0); mTextLeftPadding = (int) ta.getDimension(R.styleable.HorizontalProgressBar_hpb_textLeftPadding, Utils.sp2px(context, 5)); mTextRightPadding = (int) ta.getDimension(R.styleable.HorizontalProgressBar_hpb_textRightPadding, Utils.sp2px(context, 5)); mTextTopPadding = (int) ta.getDimension(R.styleable.HorizontalProgressBar_hpb_textTopPadding, Utils.sp2px(context, 5)); mTextBottomPadding = (int) ta.getDimension(R.styleable.HorizontalProgressBar_hpb_textBottomPadding, Utils.sp2px(context, 5)); mCenterPadding = (int) ta.getDimension(R.styleable.HorizontalProgressBar_hpb_centerPadding, Utils.sp2px(context, 5)); mProgressHeight = (int) ta.getDimension(R.styleable.HorizontalProgressBar_hpb_progressBarHeight, Utils.sp2px(context, 2)); mBackgroundColor = ta.getColor(R.styleable.HorizontalProgressBar_hpb_progressBarBackgroundColor, Color.parseColor("#E8E8E8")); mForegroundColor = ta.getColor(R.styleable.HorizontalProgressBar_hpb_progressBarForegroundColor, Color.parseColor("#912CEE")); ta.recycle(); //進度條條背景色畫筆初始化 mBackgroundPaint = new Paint(); mBackgroundPaint.setColor(mBackgroundColor); mBackgroundPaint.setStrokeCap(Paint.Cap.ROUND); mBackgroundPaint.setStrokeWidth(mProgressHeight); mBackgroundPaint.setAntiAlias(true); //指示器Path畫筆初始化 mPathPaint = new Paint(); mPathPaint.setColor(mForegroundColor); mPathPaint.setStrokeCap(Paint.Cap.ROUND); mPathPaint.setPathEffect(new CornerPathEffect(Utils.dp2px(getContext(), 2))); mPathPaint.setAntiAlias(true); //進度條前景色畫筆初始化 mProgressPaint = new Paint(); mProgressPaint.setColor(mForegroundColor); mProgressPaint.setStrokeWidth(mProgressHeight); mProgressPaint.setStrokeCap(Paint.Cap.ROUND); mProgressPaint.setAntiAlias(true); //進度值文案畫筆初始化 mTextPaint = new Paint(); mTextPaint.setColor(mTextColor); mTextPaint.setTextSize(mTextSize); mTextPaint.setStyle(Paint.Style.FILL); //指示器Path初始化 mPath = new Path();
測量
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height)); } /** * 測量寬度 * * @param mode * @param width * @return */ private int measureWidth(int mode, int width) { switch (mode) { case MeasureSpec.UNSPECIFIED: case MeasureSpec.AT_MOST: case MeasureSpec.EXACTLY: mWidth = width; break; } return mWidth; } /** * 測量高度 * * @param mode * @param height * @return */ private int measureHeight(int mode, int height) { switch (mode) { case MeasureSpec.UNSPECIFIED: case MeasureSpec.AT_MOST: case MeasureSpec.EXACTLY: mHeight = height; break; } return mHeight; } //文字測量 private void measureText(String text) { Rect rect = new Rect(); mTextPaint.getTextBounds(text, 0, text.length(), rect); mTextWidth = rect.width(); mTextHeight = rect.height(); }
繪制
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mIndicatorWidth = mTextLeftPadding + mTextWidth + mTextRightPadding; mIndicatorHeight = mTextTopPadding + mTextHeight + mTextBottomPadding; float backgroundProgressBarStartX = (float) (1.0 * mIndicatorWidth / 2); float backgroundProgressBarEndX = mWidth - mIndicatorWidth / 2; float foregroundProgress = (float) (1.0 * (mWidth - mIndicatorWidth) * currentProgress / 100); float foregroundProgressBarStartX = (float) (1.0 * mIndicatorWidth / 2); float foregroundProgressBarEndX = foregroundProgressBarStartX + foregroundProgress; Log.e(TAG, "backgroundProgressBarStartX----" + backgroundProgressBarStartX + "----backgroundProgressBarEndX----" + backgroundProgressBarEndX + "----foregroundProgress----" + foregroundProgress + "----foregroundProgressBarStartX----" + foregroundProgressBarStartX + "----foregroundProgressBarEndX----" + foregroundProgressBarEndX ); float progressX = foregroundProgress; float progressY = (float) (mCenterPadding + mIndicatorHeight * 1.5); //進度背景 canvas.drawLine(backgroundProgressBarStartX, progressY, backgroundProgressBarEndX, progressY, mBackgroundPaint); //進度前景色 canvas.drawLine(foregroundProgressBarStartX, progressY, foregroundProgressBarEndX, progressY, mProgressPaint); //指示器 drawPath(canvas, progressX, 45); //文字 drawText(canvas, mTextProgress, progressX); }
1. 指示器繪制
通過Path進行繪制,描繪出矩形以及下三角
/** * 繪制指示器 * * @param canvas * @param progressX */ private void drawPath(Canvas canvas, float progressX, float rotateAngle) { mPath.reset(); mPath.moveTo(progressX, 0); //1 mPath.lineTo(progressX + mIndicatorWidth, 0); //2 mPath.lineTo(progressX + mIndicatorWidth, mIndicatorHeight); //3 mPath.lineTo((float) (progressX + mIndicatorWidth * 0.75), mIndicatorHeight); //4 mPath.lineTo((float) (progressX + mIndicatorWidth * 0.5), (float) (mIndicatorHeight * 1.5)); //5 mPath.lineTo((float) (progressX + mIndicatorWidth * 0.25), mIndicatorHeight); //6 mPath.lineTo(progressX, mIndicatorHeight);//7 mPath.close(); canvas.drawPath(mPath, mPathPaint); }
2. 文字的繪制
文字的繪制涉及到測量上邊測量中已經(jīng)提及,但是在繪制中有涉及到基線獲取如下所示
/** * 計算繪制文字時的基線到中軸線的距離 * * @param p * @return 基線和centerY的距離 */ public float getBaseline(Paint p) { Paint.FontMetrics fontMetrics = p.getFontMetrics(); return (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent; } /** * 繪制文字 * * @param canvas * @param text * @param progressX */ private void drawText(Canvas canvas, String text, float progressX) { float baseline = getBaseline(mTextPaint); float textY = mTextTopPadding + mTextHeight / 2 + baseline; canvas.drawText(text, progressX + mTextTopPadding, textY, mTextPaint); }
總結(jié)
以上的完整代碼已經(jīng)上傳至github,github地址,如有需要可以上去看看,也可以下載直接使用或者是通過jcenter引入,由于jcenter現(xiàn)狀已經(jīng)停止維護,所以后續(xù)會遷入maven
以上就是Android實現(xiàn)帶有指示器的進度條的詳細內(nèi)容,更多關于Android 指示器進度條的資料請關注腳本之家其它相關文章!
相關文章
Android開發(fā)基于Drawable實現(xiàn)圓角矩形的方法
這篇文章主要介紹了Android開發(fā)基于Drawable實現(xiàn)圓角矩形的方法,結(jié)合實例形式分析了Drawable的功能、相關圖形繪制函數(shù)與使用方法,需要的朋友可以參考下2017-10-10Android序列化接口Parcelable與Serializable接口對比
我們使用 Intent 傳遞數(shù)據(jù)的時候,putExtra() 所支持的數(shù)據(jù)類型事有限的,當需要傳遞自定義對象的時候就需要序列化。Serializable更簡單但是會把整個對象進行序列化因此效率比Parcelable低一些2023-02-02