Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

手寫 Android 自定義圓形進度條:從繪製思路到完整實現

編輯:Android 資訊

偶然看到一個圓形進度效果的控件,感覺清爽,於是決定自己實現一版。最終效果如下:

整個思路其實是用「遮擋」模擬進度填充,拆成六步:

  1. 先畫一個實心圓;
  2. 再畫一個白色實心矩形,把圓的一半蓋住;
  3. 在圆心處繪製當前百分比文字;
  4. 用與實心圓相同的顏色,畫一個空心圓描邊;
  5. 遞增百分比數值;
  6. 根據百分比同步收縮白色矩形(抬高它的底邊 y 坐標),持續重繪直到 100%。

一、定義自定義屬性

在 values 目錄下新建 attrs.xml,聲明圓的背景色與半徑兩個屬性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="circlecolor" format="color" />
    <attr name="half" format="dimension" />
    <declare-styleable name="myCircleImage">
        <attr name="circlecolor" />
        <attr name="half" />
    </declare-styleable>
</resources>

二、自定義 View

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

public class CirclePro extends View {

    private Paint paint;
    private int circleBack;      // 圓的背景色
    private int mschedual = 0;   // 控制動態變化的進度值
    float circleHalf;            // 圓的半徑
    String percent = "";         // 繪製百分比的字符串

    @SuppressLint("Recycle")
    public CirclePro(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        TypedArray array = context.getTheme()
                .obtainStyledAttributes(attrs, R.styleable.myCircleImage, defStyleAttr, 0);
        // 注意這裡是 R.styleable.myCircleImage_circlecolor,而不是 R.attr.circlecolor
        circleBack = array.getColor(R.styleable.myCircleImage_circlecolor, Color.GREEN);
        circleHalf = array.getDimension(R.styleable.myCircleImage_half, 200.f);
    }

    /** 布局文件中引用該 view 時,必須重寫這個構造函數 */
    public CirclePro(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /** 測量文本寬度,用於讓百分比文字水平居中 */
    public float getTextWidth(String text, float textSize) {
        TextPaint textPaint = new TextPaint();
        textPaint.setTextSize(textSize);
        return textPaint.measureText(text);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float height = getHeight();
        float width = getWidth();

        // 1. 實心圓
        paint.setColor(circleBack);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(width / 2, height / 2, circleHalf, paint);

        // 2. 白色矩形遮擋,矩形底邊隨進度上移
        if (mschedual <= 100) {
            paint.setColor(Color.WHITE);
            canvas.drawRect(width / 2 - circleHalf, height / 2 - circleHalf,
                    width / 2 + circleHalf,
                    height / 2 + circleHalf - mschedual * circleHalf / 50, paint);
        }

        // 3. 百分比文字
        paint.setColor(Color.BLACK);
        paint.setTextSize(30.f);
        percent = mschedual + " %";
        canvas.drawText(percent,
                width / 2 - getTextWidth(percent, 30) / 2,
                height / 2 + paint.getTextSize() * 3 / 8, paint);

        // 4. 空心圓描邊
        paint.setColor(circleBack);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(width / 2, height / 2, circleHalf, paint);

        // 5. 遞增並重繪
        if (mschedual < 100) {
            mschedual++;
            invalidate();
        }
    }
}

三、在布局中引用

使用自定義屬性前,先在根布局聲明命名空間:

xmlns:liu="https://schemas.android.com/apk/res/com.example.androidcirclepro"

其中 liu 是隨意命名的前綴,後面跟的是應用包名。完整布局如下:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    xmlns:liu="https://schemas.android.com/apk/res/com.example.androidcirclepro"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.example.androidcirclepro.CirclePro
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        liu:half="90dp"
        liu:circlecolor="#fff0f0" />

</RelativeLayout>

至此,一個可配置顏色與半徑的圓形進度條就完成了。核心只有一句話:用一個不斷收縮的白色矩形去「吃」掉實心圓,視覺上就得到了進度填充的效果。

如果希望進度能從外部控制、或者做成順時針扇形填充,可以在後續版本中把 mschedual 改為由 setProgress() 驅動,並用 Canvas.drawArc 替代遮擋方案,效果會更平滑。

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