android環狀顯示百分比、環狀百分比檢視實現
阿新 • • 發佈:2018-12-10
效果圖:
百分比下帶文字
只顯示百分比
實現:
自定義一個View,重寫onDraw方法:
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mWidth = getWidth(); mHeight = getHeight(); if(mWidth > mHeight){ mWidth = mHeight; } mPaint.setAntiAlias(true); // 消除鋸齒 int halfWidth = mWidth/6; mPaint.setStrokeWidth(mProgressWidth); mPaint.setColor(mProgressRoundBgColor); mPaint.setStyle(Paint.Style.STROKE); RectF oval = new RectF(new Rect(halfWidth+mPaddingX, halfWidth, halfWidth*5+mPaddingX, halfWidth*5)); canvas.drawArc(oval, 0, 360, false, mPaint); mPaint.setColor(mProgressRoundColor); canvas.drawArc(oval, -90, -360*mProgress/mMax, false, mPaint); halfWidth = mWidth/5; mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(mCenterRoundColor); oval = new RectF(new Rect(halfWidth+mPaddingX, halfWidth, halfWidth*4+mPaddingX, halfWidth*4)); canvas.drawArc(oval, 0, 360, false, mPaint); mPaint.reset(); if(TextUtils.isEmpty(mBelowText)) { mPaint.setTextSize(mPencentTextSize); mPaint.setColor(mTextColor); mPaint.setStyle(Paint.Style.FILL); mPaint.setTextAlign(Paint.Align.CENTER); String number = String.format("%.0f", mProgress * 100 / mMax); canvas.drawText(number, mWidth / 2 + mPaddingX - 25, (mHeight / 2 + mPencentTextSize / 3), mPaint); float textWidth = mPaint.measureText(number); mPaint.setTextSize(mPencentTextSize); canvas.drawText("%", mWidth / 2 + mPaddingX + textWidth / 2 + 10, (mHeight / 2 + mPencentTextSize / 3), mPaint); }else{ mPaint.setTextSize(mPencentTextSize); mPaint.setColor(mTextColor); mPaint.setStyle(Paint.Style.FILL); mPaint.setTextAlign(Paint.Align.CENTER); String number = String.format("%.0f", mProgress * 100 / mMax); canvas.drawText(number, mWidth / 2 + mPaddingX - 25, (mHeight / 2 + mPencentTextSize / 3)-20, mPaint); mPaint.setColor(Color.parseColor("#C2C2C2")); mPaint.setTextSize(DensityUtil.dip2px(mContext,12f)); canvas.drawText(mBelowText,(mWidth / 2 + mPaddingX - 25)+23,(mHeight / 2 + mPencentTextSize / 3)+20,mPaint); float textWidth = mPaint.measureText(number); mPaint.setTextSize(mPencentTextSize); mPaint.setColor(mTextColor); canvas.drawText("%", mWidth / 2 + mPaddingX + textWidth / 2 + 10, (mHeight / 2 + mPencentTextSize / 3)-20, mPaint); } }
原始碼及使用程式碼下載:
博主上傳資源下載連結:
自制免費無廣告小說閱讀APP下載:
全屏播放視訊不拉伸原始碼:
科大訊飛語音評測服務接入原始碼:
android餃子播放器使用原始碼:
視訊播放前顯示視訊第一幀原始碼: