Android自定義圓弧進(jìn)度條加數(shù)字動態(tài)變化
本文實例為大家分享了Android自定義圓弧進(jìn)度條數(shù)字變化的具體代碼,供大家參考,具體內(nèi)容如下
效果如下:

思路:一個內(nèi)環(huán)圓弧和一個外環(huán)圓弧,因為有一個圓圈是在圓弧上做圓周運動,所以在畫圓的時候必須要得到圓弧上的各個點的坐標(biāo),這里其實就用到了PathMeasure這個類,可以幫我們拿到這些點,在畫圓弧的時候也理所應(yīng)當(dāng)?shù)囊褂胮ath,然后根據(jù)外界動態(tài)的傳值進(jìn)行重繪就能達(dá)到動態(tài)的效果
代碼如下:
public class ProgressPathRainbow extends View {
private Paint outPaint;
private Paint innerPaint;
private Paint mTextPaint;
private Paint mRmbTextPaint;
private int mBorderWidth = 40;
private int mCircleRadius = 40;
private int mCurrentProgress = 0;
private int mMaxProgress = 0;
private int startAngle = 180;
private int sweepAngels = 180;
private Paint mCirclePaint;
private String rmb = "¥";
private String currentText = "0.0";
public void setCurrentText(String currentText) {
this.currentText = currentText;
}
//儲存位置點
private float[] pos =new float[2];
public ProgressPathRainbow(Context context) {
super(context);
initPaint();
}
public ProgressPathRainbow(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
}
public ProgressPathRainbow(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
private void initPaint(){
outPaint = new Paint();
outPaint.setColor(0xFFECECEC);
outPaint.setAntiAlias(true);
outPaint.setStyle(Paint.Style.STROKE);
outPaint.setStrokeCap(Paint.Cap.ROUND);
outPaint.setStrokeWidth(mBorderWidth);
//
innerPaint = new Paint();
innerPaint.setColor(0xffFBA123);
innerPaint.setAntiAlias(true);
innerPaint.setStyle(Paint.Style.STROKE);
innerPaint.setStrokeCap(Paint.Cap.ROUND);
innerPaint.setStrokeWidth(mBorderWidth);
mCirclePaint = new Paint();
mCirclePaint.setColor(Color.WHITE);
mCirclePaint.setStyle(Paint.Style.FILL);
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setColor(0xffE5423D);
mTextPaint.setFakeBoldText(true);
mTextPaint.setTextSize(SizeUtils.sp2px(42));
mRmbTextPaint = new Paint();
mRmbTextPaint.setAntiAlias(true);
mRmbTextPaint.setColor(0xffE5423D);
mRmbTextPaint.setTextSize(SizeUtils.sp2px(18));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (width >= height){
setMeasuredDimension(height,height);
}else {
setMeasuredDimension(width,width);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF rectF = new RectF(mBorderWidth,mBorderWidth,getWidth()-mBorderWidth,getHeight()-mBorderWidth);
//畫內(nèi)環(huán)圓弧
Path outerPath = new Path();
outerPath.arcTo(rectF,startAngle,sweepAngels);
canvas.drawPath(outerPath,outPaint);
//畫外環(huán)圓弧
Path innerPah = new Path();
float percent = (float)mCurrentProgress/mMaxProgress;
innerPah.arcTo(rectF,startAngle,percent*sweepAngels);
canvas.drawPath(innerPah,innerPaint);
//畫金額
String tempText = new BigDecimal(currentText).multiply(new BigDecimal(percent)).setScale(1, RoundingMode.HALF_UP).toString();
Rect textBounds = new Rect();
mTextPaint.getTextBounds(tempText, 0, tempText.length(), textBounds);
int dx = getWidth()/2 - textBounds.width()/2;
// 基線 baseLine
Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
int baseLine = getHeight()/3 + dy;
canvas.drawText(tempText,dx,baseLine,mTextPaint);
//畫人民幣符號
Rect textBoundRmbs = new Rect();
mTextPaint.getTextBounds(rmb, 0, rmb.length(), textBoundRmbs);
int dxRmb = dx-50;
// 基線 baseLine
Paint.FontMetricsInt fontMetricsRmb = mTextPaint.getFontMetricsInt();
int dyRmb = (fontMetricsRmb.bottom - fontMetricsRmb.top)/2 - fontMetricsRmb.bottom;
int baseLineRmb = getHeight()/3 + dyRmb;
canvas.drawText(rmb,dxRmb,baseLineRmb,mRmbTextPaint);
//獲取圓弧上點的位置(坐標(biāo),畫一個圓)
PathMeasure pathMeasure = new PathMeasure(outerPath,false);
boolean posTan = pathMeasure.getPosTan(pathMeasure.getLength() * percent, pos, null);
canvas.drawCircle(pos[0],pos[1],mCircleRadius,mCirclePaint);
}
public synchronized void setmCurrentProgress(int mCurrentProgress) {
this.mCurrentProgress = mCurrentProgress;
invalidate();
}
public synchronized void setmMaxProgress(int mMaxProgress) {
this.mMaxProgress = mMaxProgress;
}
}
以上就可以實現(xiàn)這個效果
使用的話可以這樣
detailRainbowPr.setmMaxProgress(100);
detailRainbowPr.setCurrentText("99.9");
ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, 100);
valueAnimator.setDuration(5000);
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.addUpdateListener(valueAnimator1 -> {
float step = (float) valueAnimator1.getAnimatedValue();
detailRainbowPr.setmCurrentProgress((int) step);
});
valueAnimator.start();
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android progressbar實現(xiàn)帶底部指示器和文字的進(jìn)度條
- Android Studio實現(xiàn)進(jìn)度條效果
- Android ProgressBar 模擬進(jìn)度條效果的實現(xiàn)
- Android自定義分段式進(jìn)度條
- Android自定義圓形進(jìn)度條效果
- Android seekbar實現(xiàn)可拖動進(jìn)度條
- Android自定義View實現(xiàn)圓形進(jìn)度條
- Android實現(xiàn)進(jìn)度條(ProgressBar)的功能與用法
- android自定義等級評分圓形進(jìn)度條
- Android自定義控件之圓形進(jìn)度條動畫
- Android 進(jìn)度條自動前進(jìn)效果的實現(xiàn)代碼
- Android實現(xiàn)帶有指示器的進(jìn)度條
相關(guān)文章
Android viewpager無限輪播獲取網(wǎng)絡(luò)圖片功能
這篇文章主要為大家詳細(xì)介紹了Android viewpager無限輪播獲取網(wǎng)絡(luò)圖片功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
Android高級組件AutoCompleteTextView自動完成文本框使用詳解
這篇文章主要介紹了Android高級組件AutoCompleteTextView自動完成文本框的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放
本篇文章實現(xiàn)了Android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
Android動態(tài)加載布局實現(xiàn)技巧介紹
通過使用LayoutInflater 每次點擊按鈕時候去讀取布局文件,然后找到布局文件里面的各個VIEW 操作完VIEW 后加載進(jìn)我們setContentView 方面里面的要放的布局文件里面,每次動態(tài)加載文件必需調(diào)用 removeAllViews方法,清除之前的加載進(jìn)來的View2022-12-12
詳解AndroidStudio中代碼重構(gòu)菜單Refactor功能
這篇文章主要介紹了AndroidStudio中代碼重構(gòu)菜單Refactor功能詳解,本文通過代碼演示,功能截圖來詳細(xì)說明as為大名重構(gòu)提供的各項功能,需要的朋友可以參考下2019-11-11

