編輯:關於 Android 程式設計
本文介紹如何自定義一個 Material 風格的環形進度條 CircleProgressBar,先看效果:

在布局中直接使用自定義 View,並通過自定義屬性配置顏色與描邊:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical">
<com.example.mytest.view.CircleProgressBar
android:id="@+id/progress1"
android:layout_width="60dp"
android:layout_height="60dp"
app:mlpb_progress_color="#566da9"
app:mlpb_progress_stoke_width="3dp" />
</LinearLayout>
在 attrs.xml 中聲明 CircleProgressBar 支持的屬性:
<declare-styleable name="CircleProgressBar">
<attr name="mlpb_inner_radius" format="dimension" />
<attr name="mlpb_background_color" format="color" />
<attr name="mlpb_progress_color" format="color" />
<attr name="mlpb_progress_stoke_width" format="dimension" />
<attr name="mlpb_show_arrow" format="boolean" />
<attr name="mlpb_enable_circle_background" format="boolean" />
<attr name="mlpb_arrow_width" format="dimension" />
<attr name="mlpb_arrow_height" format="dimension" />
<attr name="mlpb_progress" format="integer" />
<attr name="mlpb_max" format="integer" />
<attr name="mlpb_progress_text_size" format="dimension" />
<attr name="mlpb_progress_text_color" format="color" />
<attr name="mlpb_progress_text_visibility" format="enum">
<enum name="visible" value="0" />
<enum name="invisible" value="1" />
</attr>
</declare-styleable>
CircleProgressBar 繼承自 ImageView,內部通過 MaterialProgressDrawable 繪製轉圈動畫。關鍵在於在 onLayout 中計算尺寸並初始化 Drawable:
public class CircleProgressBar extends ImageView {
private int mBackGroundColor;
private int mProgressColor;
private int mProgressStokeWidth;
private int mDiameter;
private int mInnerRadius;
private int mProgress;
private int mMax;
private boolean mIfDrawText;
private boolean mShowArrow;
private boolean mCircleBackgroundEnabled;
private int[] mColors = new int[]{Color.BLACK};
private MaterialProgressDrawable mProgressDrawable;
private Paint mTextPaint;
private void init(Context context, AttributeSet attrs) {
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.CircleProgressBar, 0, 0);
final float density = getResources().getDisplayMetrics().density;
mBackGroundColor = a.getColor(
R.styleable.CircleProgressBar_mlpb_background_color, 0xFFFAFAFA);
mProgressColor = a.getColor(
R.styleable.CircleProgressBar_mlpb_progress_color, 0xFFFAFAFA);
mColors = new int[]{mProgressColor};
mInnerRadius = a.getDimensionPixelOffset(
R.styleable.CircleProgressBar_mlpb_inner_radius, -1);
mProgressStokeWidth = a.getDimensionPixelOffset(
R.styleable.CircleProgressBar_mlpb_progress_stoke_width, (int) (3 * density));
mProgress = a.getInt(R.styleable.CircleProgressBar_mlpb_progress, 0);
mMax = a.getInt(R.styleable.CircleProgressBar_mlpb_max, 100);
mShowArrow = a.getBoolean(R.styleable.CircleProgressBar_mlpb_show_arrow, false);
mCircleBackgroundEnabled = a.getBoolean(
R.styleable.CircleProgressBar_mlpb_enable_circle_background, true);
int textVisible = a.getInt(
R.styleable.CircleProgressBar_mlpb_progress_text_visibility, 1);
if (textVisible != 1) mIfDrawText = true;
mTextPaint = new Paint();
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setColor(Color.BLACK);
mTextPaint.setAntiAlias(true);
a.recycle();
mProgressDrawable = new MaterialProgressDrawable(getContext(), this);
super.setImageDrawable(mProgressDrawable);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mDiameter = Math.min(getMeasuredWidth(), getMeasuredHeight());
mProgressDrawable.setColorSchemeColors(mColors);
mProgressDrawable.setSizeParameters(
mDiameter, mDiameter,
mInnerRadius <= 0 ? (mDiameter - mProgressStokeWidth * 2) / 4 : mInnerRadius,
mProgressStokeWidth, mProgressStokeWidth * 4, mProgressStokeWidth * 2);
super.setImageDrawable(null);
super.setImageDrawable(mProgressDrawable);
mProgressDrawable.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mIfDrawText) {
String text = String.format("%s%%", mProgress);
int x = getWidth() / 2 - text.length() * 8 / 4;
int y = getHeight() / 2 + 8 / 4;
canvas.drawText(text, x, y, mTextPaint);
}
}
public void setColorSchemeColors(int... colors) {
mColors = colors;
if (mProgressDrawable != null) mProgressDrawable.setColorSchemeColors(colors);
}
public void setProgress(int progress) {
if (mMax > 0) mProgress = progress;
}
}
這裡的 MaterialProgressDrawable 來自官方 support 庫的 Material 進度動畫實現,你也可以直接依賴官方庫來簡化。
CircleProgressBar progress1 = findViewById(R.id.progress1);
progress1.setColorSchemeResources(
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
通過繼承 ImageView 並封裝 MaterialProgressDrawable,我們實現了一個可自定義顏色、描邊、內圈半徑與文字顯示的環形進度條。實際開發中,也可以直接參考或引入成熟的 Material 進度庫,達到同樣的效果。
OpenGL ES 紋理繪製入門(一)
矩陣變換類 package test.com.opengles7_1; import android.opengl.Matrix; /** * Created by h
Dragonboard 410c 從零開始:環境建置與 Sensor Demo 實作
前言Dragonboard 410c 是一款可執行 Android 的開發板。本文適合第一次接觸它的讀者,從裸板環境開始,說明如何燒寫系統、安裝 Sensor Demo,
Cocos2d-x 3.2 Lua 範例:ClickAndMoveTest(點擊移動測試)
本篇介紹 Cocos2d-x 3.2 Lua 範例中的「點擊移動」實作。透過這個例子,你可以學到如何創建單點觸摸事件,以及註冊事件監聽的回呼方法。 範例程式碼 --[[
小米 2S 技巧:兩種快捷截屏方法
小米手機如何快速截屏?以下彙整兩種小米手機快速截屏的方法,需要的朋友趕快收藏起來試試看! 1、下拉通知欄,點擊「截屏」,2 秒後直接進入截屏並擷取當前螢幕: 2、同時按