Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義 Material 風格環形進度條

編輯:關於 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 進度庫,達到同樣的效果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved