Android 實(shí)現(xiàn)自定義圓形進(jìn)度條的功能
Android 實(shí)現(xiàn)自定義圓形進(jìn)度條:
Android 自定義view,在大多數(shù)項(xiàng)目中根據(jù)客戶需求及用戶的體驗(yàn)度來(lái)說,都要重新寫控件的來(lái)展示漂亮的界面,這里就對(duì)圓形進(jìn)度條說下如何實(shí)現(xiàn)。
繪制自定義的圓形進(jìn)度條,分為三個(gè)步驟,內(nèi)圓、外圓、文字。
其中內(nèi)圓和文字比較好繪制,進(jìn)度條的變化是由外圓來(lái)控制的,所以核心就是繪制外圓。
首先定義分別定義這三個(gè)畫筆,兩個(gè)Paint和一個(gè)TextPaint
mCirclePaint = new Paint(); mCirclePaint.setAntiAlias(true); mCirclePaint.setStrokeWidth(CIRCLE_LINE_WIDTH); mCirclePaint.setStyle(Paint.Style.STROKE); mCirclePaint.setColor(ContextCompat.getColor(context, R.color.circle_color)); mCircleInnerPaint = new Paint(); mCircleInnerPaint.setAntiAlias(true); mCircleInnerPaint.setStyle(Paint.Style.FILL); mCircleInnerPaint.setColor(ContextCompat.getColor(context, R.color.circle_inner_color)); mTextPaint = new TextPaint(); mTextPaint.setAntiAlias(true); mTextPaint.setStyle(Paint.Style.FILL); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setColor(ContextCompat.getColor(context, R.color.circle_text_color)); mTextPaint.setTextSize(TEXT_SIZE);
然后讓我們分別繪制出這三個(gè)部分獲取自定義View的寬和高
float halfWidth = getMeasuredWidth() / 2;
float halfHeight = getMeasuredHeight() / 2;
繪制外圓
canvas.drawCircle(halfWidth, halfHeight, CIRCLE_RADIUS,mCirclePaint);
繪制內(nèi)圓
canvas.drawCircle(halfWidth, halfHeight,CIRCLE_RADIUS - CIRCLE_LINE_WIDTH / 2,mCircleInnerPaint);
繪制文字
canvas.drawText(mProgressText,halfWidth - mTextPaint.measureText(mProgressText) / 2,halfHeight - (mTextPaint.ascent() + mTextPaint.descent()) / 2,mTextPaint);
最后的效果如下圖

繪制完了基本的圖案,下一步就是實(shí)現(xiàn)進(jìn)度條的動(dòng)畫效果
進(jìn)度條是實(shí)時(shí)變化的,所以需要不斷的去更新進(jìn)度,進(jìn)度可以用圓弧開繪制
設(shè)置進(jìn)度的方法
public void setProgress(float progress) {
if (progress > 100) {
progress = 100;
}
if (progress < 0) {
progress = 0;
}
mProgress = progress;
mProgressText = "Pause";
mStartProgress = true;
postInvalidate();
}
在Activity中開一個(gè)線程模擬網(wǎng)絡(luò)請(qǐng)求后更新進(jìn)度條的操作
沒30毫秒更新一次數(shù)據(jù),當(dāng)進(jìn)度超過100,停止刷新界面
private void startProgress() {
new Thread() {
@Override
public void run() {
super.run();
float currentProgress = mCustomView.getCurrentProgress();
++currentProgress;
mCustomView.setProgress(currentProgress);
try {
sleep(30);
if (currentProgress <= 100) {
startProgress();
} else {
mCustomView.progressFinished();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
最核心的部分,進(jìn)度更新后更新繪制圓形進(jìn)度條
float halfWidth = getMeasuredWidth() / 2;
float halfHeight = getMeasuredHeight() / 2;
if (null == mCircleRectF) {
mCircleRectF = new RectF(halfWidth - CIRCLE_RADIUS, halfHeight - CIRCLE_RADIUS, halfWidth + CIRCLE_RADIUS, halfHeight + CIRCLE_RADIUS);
}
if (mStartProgress) {
float swipeProgress = mProgress / 100f * 360;
LogUtils.e("swipeProgress = " + swipeProgress);
canvas.drawArc(mCircleRectF, -90, swipeProgress, true, mCirclePaint);
} else {
canvas.drawCircle(halfWidth, halfHeight, CIRCLE_RADIUS,mCirclePaint);
}
繪制的思路就是把progress進(jìn)度轉(zhuǎn)換為圓弧的弧度,然后不斷繪制出來(lái),這里要注意,從-90開始,也就是時(shí)鐘的0點(diǎn)時(shí)刻開始繪制。如果進(jìn)度已經(jīng)繪制完成,或者還沒有開始,則直接繪制一個(gè)圓形。
大概思路就是這樣,最后上兩張效果圖


如果有什么更好的實(shí)現(xiàn)思路,可以一起討論學(xué)習(xí)。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android studio 生成帶Kotlin文檔的實(shí)現(xiàn)方式
這篇文章主要介紹了Android studio 生成帶Kotlin文檔的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-03-03
基于android startActivityForResult的學(xué)習(xí)心得總結(jié)
本篇文章是對(duì)android中的startActivityForResult進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android開發(fā)中RecyclerView組件使用的一些進(jìn)階技講解
RecyclerView是Android 5.0以來(lái)新加入的一個(gè)組件,基本上全面優(yōu)于ListView,這里我們將來(lái)關(guān)注Android開發(fā)中RecyclerView組件使用的一些進(jìn)階技講解:2016-06-06
Android實(shí)現(xiàn)簡(jiǎn)單MD5加密的方法
這篇文章主要介紹了Android實(shí)現(xiàn)簡(jiǎn)單MD5加密的方法,涉及Android編碼操作及加密的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
RecyclerView進(jìn)階:使用ItemTouchHelper實(shí)現(xiàn)拖拽和側(cè)滑刪除效果
現(xiàn)在RecyclerView的應(yīng)用越來(lái)越廣泛了,本篇文章主要介紹了RecyclerView進(jìn)階:使用ItemTouchHelper實(shí)現(xiàn)拖拽和側(cè)滑刪除效果,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02
Android實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤實(shí)例代碼,可以應(yīng)用于Android游戲開發(fā)中的一個(gè)應(yīng)用,需要的朋友可以參考下2014-07-07
Android實(shí)現(xiàn)圓圈倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓圈倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

