Android自定義view-繪製圓形進度條
阿新 • • 發佈:2019-01-01
詳細可參考:http://blog.csdn.net/Beyond0525/article/details/48181345
最近專案上有一些需求,需要繪製圓形的進度條滿足設計上和互動上的需求:
實現思路
在畫布上直接繪製View,需要了解一下幾點
1.需要畫一個圓
2.圓圈上有不同進度的顏色
3.圓圈中有進度數字的展示
4.圓圈中間還有可以自定義不同文案提示
一、畫圓
需要使用Canvas的該方法
public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter,
@NonNull Paint paint) {
drawArc(oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle, useCenter,
paint);
}
如下:
// 設定畫筆相關屬性
mPaint.setAntiAlias(true);
mPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
canvas.drawColor(Color.TRANSPARENT);
mPaint.setStrokeWidth(mCircleLineStrokeWidth);
mPaint.setStyle(Style.STROKE);
// 位置
mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x
mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y
mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x
mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y
// 繪製圓圈,進度條背景
canvas.drawArc(mRectF, -90, 360, false, mPaint);
此時畫出了預設的背景圓圈:
二、畫進度圓弧
其實實現很簡單,換另外一種顏色同樣在畫布上畫出即可,支援此時畫的不是360°,而是通過進度計算出來的一個圓弧。
mPaint.setColor(Color.rgb(0xf8, 0x60, 0x30));
canvas.drawArc (mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);
如上即可,此時mProgress / mMaxProgress = 80/100;即可繪製出如下效果
三、畫中間進度百分比
其實要用到Canvas中繪製文字的方法:
public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) {
native_drawText(mNativeCanvasWrapper, text, 0, text.length(), x, y, paint.mBidiFlags,
paint.mNativePaint, paint.mNativeTypeface);
}
如下:
// 繪製進度文案顯示
mPaint.setStrokeWidth(mTxtStrokeWidth);
String text = mProgress + "%";
int textHeight = height / 4;
mPaint.setTextSize(textHeight);
int textWidth = (int) mPaint.measureText(text, 0, text.length());
mPaint.setStyle(Style.FILL);
canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, mPaint);
主要要計算好畫的位置,drawText中對應就是相關的位置,相當於居中顯示進度百分比文案。效果如下:
四、畫圓圈中間提示文案
方法同三,只不過要計算出畫的位置即可,效果圖如下:
五、總結
其實很多自定義的View都可以用Canvas直接畫出來,如果專案上有類似這樣的需求,可以先研究Canvas的使用和原理。
最後附上原始碼:
public class CircleProgressView extends View {
private static final String TAG = "CircleProgressBar";
private int mMaxProgress = 100;
private int mProgress = 30;
private final int mCircleLineStrokeWidth = 8;
private final int mTxtStrokeWidth = 2;
// 畫圓所在的距形區域
private final RectF mRectF;
private final Paint mPaint;
private final Context mContext;
private String mTxtHint1;
private String mTxtHint2;
public CircleProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mRectF = new RectF();
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = this.getWidth();
int height = this.getHeight();
if (width != height) {
int min = Math.min(width, height);
width = min;
height = min;
}
// 設定畫筆相關屬性
mPaint.setAntiAlias(true);
mPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
canvas.drawColor(Color.TRANSPARENT);
mPaint.setStrokeWidth(mCircleLineStrokeWidth);
mPaint.setStyle(Style.STROKE);
// 位置
mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x
mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y
mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x
mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y
// 繪製圓圈,進度條背景
canvas.drawArc(mRectF, -90, 360, false, mPaint);
mPaint.setColor(Color.rgb(0xf8, 0x60, 0x30));
canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);
// 繪製進度文案顯示
mPaint.setStrokeWidth(mTxtStrokeWidth);
String text = mProgress + "%";
int textHeight = height / 4;
mPaint.setTextSize(textHeight);
int textWidth = (int) mPaint.measureText(text, 0, text.length());
mPaint.setStyle(Style.FILL);
canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, mPaint);
if (!TextUtils.isEmpty(mTxtHint1)) {
mPaint.setStrokeWidth(mTxtStrokeWidth);
text = mTxtHint1;
textHeight = height / 8;
mPaint.setTextSize(textHeight);
mPaint.setColor(Color.rgb(0x99, 0x99, 0x99));
textWidth = (int) mPaint.measureText(text, 0, text.length());
mPaint.setStyle(Style.FILL);
canvas.drawText(text, width / 2 - textWidth / 2, height / 4 + textHeight / 2, mPaint);
}
if (!TextUtils.isEmpty(mTxtHint2)) {
mPaint.setStrokeWidth(mTxtStrokeWidth);
text = mTxtHint2;
textHeight = height / 8;
mPaint.setTextSize(textHeight);
textWidth = (int) mPaint.measureText(text, 0, text.length());
mPaint.setStyle(Style.FILL);
canvas.drawText(text, width / 2 - textWidth / 2, 3 * height / 4 + textHeight / 2, mPaint);
}
}
public int getMaxProgress() {
return mMaxProgress;
}
public void setMaxProgress(int maxProgress) {
this.mMaxProgress = maxProgress;
}
public void setProgress(int progress) {
this.mProgress = progress;
this.invalidate();
}
public void setProgressNotInUiThread(int progress) {
this.mProgress = progress;
this.postInvalidate();
}
public String getmTxtHint1() {
return mTxtHint1;
}
public void setmTxtHint1(String mTxtHint1) {
this.mTxtHint1 = mTxtHint1;
}
public String getmTxtHint2() {
return mTxtHint2;
}
public void setmTxtHint2(String mTxtHint2) {
this.mTxtHint2 = mTxtHint2;
}
}
Xml中配置:
<com.jackshy.demo.view.CircleProgressBar
android:id="@+id/circleProgressbar"
android:layout_width="74dp"
android:layout_height="74dp"
android:layout_centerInParent="true" />
MainActivity中使用:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.jackshy.demo.view.CircleProgressBar;
public class MainActivity extends Activity {
private CircleProgressBar mCircleBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initViews() {
mCircleBar = (CircleProgressBar) findViewById(R.id.circleProgressbar);
mCircleBar.setProgress(80);
}
}
上述還可以做成進度條的形式,這時候需要啟動非UI執行緒呼叫此方法即可:
public void setProgressNotInUiThread(int progress) {
this.mProgress = progress;
this.postInvalidate();
}