創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
這篇文章主要講解了Android自定義圓弧進(jìn)度條加數(shù)字動(dòng)態(tài)變化的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
效果如下:
思路:一個(gè)內(nèi)環(huán)圓弧和一個(gè)外環(huán)圓弧,因?yàn)橛幸粋€(gè)圓圈是在圓弧上做圓周運(yùn)動(dòng),所以在畫圓的時(shí)候必須要得到圓弧上的各個(gè)點(diǎn)的坐標(biāo),這里其實(shí)就用到了PathMeasure這個(gè)類,可以幫我們拿到這些點(diǎn),在畫圓弧的時(shí)候也理所應(yīng)當(dāng)?shù)囊褂胮ath,然后根據(jù)外界動(dòng)態(tài)的傳值進(jìn)行重繪就能達(dá)到動(dòng)態(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; } //儲(chǔ)存位置點(diǎn) 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); //畫人民幣符號(hào) 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); //獲取圓弧上點(diǎn)的位置(坐標(biāo),畫一個(gè)圓) 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; } }
網(wǎng)頁(yè)標(biāo)題:Android自定義圓弧進(jìn)度條加數(shù)字動(dòng)態(tài)變化的方法-創(chuàng)新互聯(lián)
新聞來(lái)源:http://chinadenli.net/article2/dhpsic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、App設(shè)計(jì)、網(wǎng)站排名、ChatGPT、品牌網(wǎng)站制作、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容