項目中用到這種自定義的進(jìn)度條,把代碼提取出來,方便以后查找繼續(xù)使用酝陈,哈哈。
效果圖:
底部文字需要繪制圓角背景毁涉,需要用到drawRoundRect()沉帮,該方法用于在畫布上繪制圓角矩形,通過指定RectF對象以及圓角半徑來實現(xiàn)薪丁。該方法是繪制圓角矩形的主要方法遇西,同時也可以通過設(shè)置畫筆的空心效果來繪制空心的圓角矩形。
【基本語法】public void drawRoundRect (RectF rect, float rx, float ry, Paint paint)
參數(shù)說明
rect:RectF對象严嗜。
rx:x方向上的圓角半徑粱檀。
ry:y方向上的圓角半徑。
paint:繪制時所使用的畫筆漫玄。
自定義view的類CircleProgressView:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float centerX = getWidth() / 2;
float centerY = getHeight() / 2;
radius = centerX-stokeWidth/2;
canvas.drawCircle(centerX, centerY, radius, mCirPaint);
//畫圓
RectF rectF=new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
canvas.drawArc(rectF, -90, progress * 360 / 100, false, mArcPaint);
//設(shè)置中心文本
if (null != centerText) {
canvas.drawText(centerText, centerX - (mTextCenterPaint.measureText(centerText)) / 2, centerY + mTextCenterSize / 2, mTextCenterPaint);
}
//上方文字
if (null != topText) {
canvas.drawText(topText, centerX - (mTextTopPaint.measureText(topText)) / 2, centerY - mTextCenterSize, mTextTopPaint);
}
//最上方小文字
if(null !=topFloorText){
canvas.drawText(topFloorText,getWidth()-mTextCenterPaint.measureText(topFloorText),centerY-mTextCenterSize-mTextTopSize, mTextTopFloorPaint);
}
if (null != bottomText) {
//下方圓角矩形
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(bottomTextColor);
RectF rec = new RectF(centerX - (mTextBottomPaint.measureText(bottomText)) / 2-DensityUtil.dip2px(mContext,6),
getHeight()-centerY/2-mTextBottomSize,
centerX +(mTextBottomPaint.measureText(bottomText)) / 2+DensityUtil.dip2px(mContext,6),
getHeight()-(centerY)/2+mTextBottomSize/2);
canvas.drawRoundRect(rec,10, 10, paint); //第二個參數(shù)是x半徑茄蚯,第三個參數(shù)是y半徑
//下方文字
canvas.drawText(bottomText,centerX - (mTextBottomPaint.measureText(bottomText)) / 2, getHeight()-(centerY)/2, mTextBottomPaint);
}
}
布局文件代碼:
<com.mylibrary.CircleProgressView
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/progress_view"
app:unReachColor="@color/unReachColor"
app:reachColor="@color/blue"
app:topFloorTextColor="@color/colorAccent"
app:bottomTextColor="@color/blue"
app:centerTextColor="@color/blue"
app:stokeWith="20"
app:topFloorTextSize="10"
app:topTextSize="14"
app:centerTextSize="30"
app:bottomTextSize="14"
/>
activity中設(shè)置progress相關(guān)屬性
circleProgressView= (CircleProgressView) findViewById(R.id.progress_view);
circleProgressView.setTopFloorText("小文字");
circleProgressView.setTopText("上方文字");
circleProgressView.setCenterText("中間文字");
circleProgressView.setBottomText("下方文字");
circleProgressView.setProgress(80);
circleProgressView.setSpeed(40);
以后會繼續(xù)添加压彭。源碼地址:https://github.com/yinsujun/CircularProgress