編輯:關於 Android 程式設計
照著經典寫法自己做了一份當備忘,有興趣的可以對照原始版本一起看。
先在 res/values/attrs.xml 裡把要對外開放的外觀參數宣告出來,這樣後續在 XML 裡就能直接設定顏色、粗細與文字樣式。
核心在 onDraw:先畫一層灰色底環,再用 drawArc() 依進度畫出前景弧線,最後把百分比數字居中畫上去。這裡額外提供 STROKE(只有描邊)與 FILL(實心扇形)兩種風格可切換。
public class RoundProgressBar extends View {
private Paint mPaint;
private int mRoundColor; // 底環顏色
private int mProgressColor; // 進度顏色
private int mTextColor;
private int mTextSize;
private float mRounWidth;
private int mMaxProgress;
private int mCurrentProgress = 45;
private boolean mTextIsDisplayable;
private int mStyle = 1;
private static final int STROKE = 0;
private static final int FILL = 1;
public RoundProgressBar(Context context) { this(context, null); }
public RoundProgressBar(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
mRoundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
mProgressColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
mTextColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.BLUE);
mTextSize = typedArray.getDimensionPixelSize(R.styleable.RoundProgressBar_textSize, 15);
mRounWidth = typedArray.getDimensionPixelSize(R.styleable.RoundProgressBar_roundWidth, 5);
mMaxProgress = typedArray.getInt(R.styleable.RoundProgressBar_max, 100);
mTextIsDisplayable = typedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
mStyle = typedArray.getInt(R.styleable.RoundProgressBar_style, STROKE);
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int center = getWidth() / 2;
int radius = (int) (center - mRounWidth / 2);
// 底環
mPaint.setColor(mRoundColor);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(mRounWidth);
mPaint.setAntiAlias(true);
canvas.drawCircle(center, center, radius, mPaint);
// 進度弧
mPaint.setStrokeWidth(mRounWidth);
mPaint.setColor(mProgressColor);
RectF oval = new RectF(center - radius, center - radius,
center + radius, center + radius);
switch (mStyle) {
case STROKE:
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawArc(oval, 0, 360 * mCurrentProgress / mMaxProgress, false, mPaint);
break;
case FILL:
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
if (mCurrentProgress != 0) {
canvas.drawArc(oval, 0, 360 * mCurrentProgress / mMaxProgress, true, mPaint);
}
break;
}
// 中央百分比
mPaint.setStrokeWidth(0);
mPaint.setColor(mTextColor);
mPaint.setTextSize(mTextSize);
mPaint.setTypeface(Typeface.DEFAULT);
int percent = (int) (((float) mCurrentProgress / (float) mMaxProgress) * 100);
float textWidth = mPaint.measureText(percent + "%");
if (percent != 0 && mTextIsDisplayable) {
canvas.drawText(percent + "%", center - textWidth / 2,
center + mTextSize / 2, mPaint);
}
}
public int getRoundColor() { return mRoundColor; }
public void setRoundColor(int roundColor) { this.mRoundColor = roundColor; }
public int getProgressColor() { return mProgressColor; }
public void setProgressColor(int progressColor) { this.mProgressColor = progressColor; }
public int getTextColor() { return mTextColor; }
public void setTextColor(int textColor) { this.mTextColor = textColor; }
public int getTextSize() { return mTextSize; }
public void setTextSize(int textSize) { this.mTextSize = textSize; }
public float getRounWidth() { return mRounWidth; }
public void setmRounWidth(float mRounWidth) { this.mRounWidth = mRounWidth; }
public synchronized int getMaxProgress() { return mMaxProgress; }
public synchronized void setMaxProgress(int maxProgress) {
if (maxProgress < 0) {
throw new IllegalArgumentException("Max Progress not less than 0");
}
this.mMaxProgress = maxProgress;
}
public synchronized int getCurrentProgress() { return mCurrentProgress; }
public synchronized void setCurrentProgress(int currentProgress) {
if (currentProgress < 0) {
throw new IllegalArgumentException("Progress not less than 0");
}
if (currentProgress > mMaxProgress) {
currentProgress = mMaxProgress;
}
this.mCurrentProgress = currentProgress;
postInvalidate();
}
public boolean isTextIsDisplayable() { return mTextIsDisplayable; }
public void setTextDisplayable(boolean displayable) { this.mTextIsDisplayable = displayable; }
public int getStyle() { return mStyle; }
public void setStyle(int style) { this.mStyle = style; }
}
記得加上自訂命名空間 https://schemas.android.com/apk/res/com.example.demo,前綴才能認得這些屬性。
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:android_custom="https://schemas.android.com/apk/res/com.example.demo"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.demo.RoundProgressBar
android:id="@+id/roundProgressBar"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center"
android_custom:roundColor="#D1D1D1"
android_custom:roundProgressColor="@android:color/holo_orange_dark"
android_custom:roundWidth="4dip"
android_custom:textColor="#9A32CD"
android_custom:textIsDisplayable="true"
android_custom:textSize="18sp" />
</FrameLayout>


幾個可以再加強的地方:setCurrentProgress() 目前是瞬時跳變,若要平滑過渡可以搭配 ValueAnimator 做遞增動畫;另外 getWidth() 在 onDraw 中假設是正方形,若版面給的是矩形,圓會被壓扁,記得在 onMeasure 裡鎖定等寬高。
Android 頂部+底部雙導航界面實作(FragmentTabHost + ViewPager)
一直想試試雙層導航,翻了不少資料總算跑通了,這篇就当筆記記錄下來。 底部導航:FragmentTabHost + Fragment 底部我用 FragmentTabH
Android AppCompatActivity 與 Toolbar 整合指南
從 AppCompatActivity 開始在 Android 5.0 之後,Toolbar 逐漸取代 ActionBar,而 AppCompatActivity 也成為
Android 基礎總結:Activity 的四種 launchMode 詳解
本篇要講的是 Activity 的四種啟動模式 launchMode。在多個 Activity 相互跳轉的場景中,launchMode 扮演著至關重要的角色:它決定了系統
Android 啟動頁漸變動畫的實現思路與代碼
啟動頁(Splash / Landing)是第一印象的關鍵。與其硬邦邦地一閃而過,不如讓 Logo 由透明漸變到清晰,再平穩過渡到主界面。這裡記錄一種簡單可行的實現方式,