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

Android UI 實作(四):雲通訊錄的雲端更新圓形進度條

編輯:關於 Android 程式設計

作者:泥沙磚瓦漿木匠 網站:https://blog.csdn.net/jeffli1993

個人簽名:打算起手不凡寫出鴻篇巨作的人,往往堅持不了完成第一章節。

交流 QQ 群:【編程之美 365234583】

前言

繼續 Android UI 系列。UI 說到底是一門前端的活,美感才是最終的 boss 階段。這一輪也想順便把 Android 的一些簡單入門寫下來,鞏固提升一下觀念。

有道是「路漫漫其修遠兮,吾將上下而求索」。任何東西都不是一步登天,注意細節、一步一步來,自然就值得了。但也請記住:做人做事第一,技術第二。

今天要做的畫面

先把結構拆開:

  • 頂部 barTop:一個 ImageView(或 Button)加一個 TextView,樣式交給 styles.xml 控制。
  • 中間是重點:自訂 View 畫一個圓形進度條。
  • 底部 ImgBottom:一張圖片收尾。

圓形進度條的原理

要做這個,得先熟悉 Canvas 與 Paint 的相關知識。Android 的 2D 繪圖 API 都在 android.graphics 與 android.graphics.drawable 兩個套件裡:圖形相關的有 Point、RectF 等,動畫相關的有 AnimationDrawable、BitmapDrawable、TransitionDrawable 等。

而這個範例用到的其實只有兩個動作:定義一個矩形區域(RectF),然後在這個區域裡畫兩段弧線——一段白色的是進度條的底,一段藍色的是實際進度。角度算好,剩下的自然就出來了。

public class CircleProgressBar extends View {
    private int maxProgress = 100;
    private int progress = 15;
    private int progressStrokeWidth = 16;
    private int marxArcStorkeWidth = 16;

    RectF oval;      // 畫圓所在的矩形區域
    Paint paint;

    public CircleProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        oval = new RectF();
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();
        width  = (width > height) ? height : width;    // 取短邊,確保是正圓
        height = (width > height) ? height : width;

        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        canvas.drawColor(Color.TRANSPARENT);

        paint.setStrokeWidth(progressStrokeWidth);
        paint.setStyle(Style.STROKE);

        // 依線寬內縮,避免圓被畫布切掉
        oval.left   = marxArcStorkeWidth / 2;
        oval.top    = marxArcStorkeWidth / 2;
        oval.right  = width  - marxArcStorkeWidth / 2;
        oval.bottom = height - marxArcStorkeWidth / 2;

        canvas.drawArc(oval, -90, 360, false, paint);        // 白色底圈

        paint.setColor(Color.rgb(0x57, 0x87, 0xb6));
        paint.setStrokeWidth(marxArcStorkeWidth);
        canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint);   // 進度弧

        paint.setStrokeWidth(1);
        String text = progress + "%";
        int textHeight = height / 4;
        paint.setTextSize(textHeight);
        int textWidth = (int) paint.measureText(text, 0, text.length());
        paint.setStyle(Style.FILL);
        canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, paint);
    }

    /** 設置進度,僅限 UI 執行緒呼叫 */
    public void setProgress(int progress, View view) {
        this.progress = progress;
        view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress)));
        this.invalidate();
    }

    /** 非 UI 執行緒呼叫 */
    public void setProgressNotInUiThread(int progress, View view) {
        this.progress = progress;
        view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress)));
        this.postInvalidate();
    }

    /** 進度標註點的旋轉動畫(圖片本身偏移約 54 度,起始角度要補回來) */
    private Animation pointRotationAnima(float fromDegrees, float toDegrees) {
        int initDegress = 300;
        RotateAnimation animation = new RotateAnimation(fromDegrees, initDegress + toDegrees,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(1);
        animation.setRepeatCount(1);
        animation.setFillAfter(true);
        return animation;
    }
}

幾個實作重點:

  • 起點寫 -90 度,讓進度從 12 點鐘方向開始順時針填。
  • oval 的邊界要依線寬內縮一半,否則圓會被畫布切掉一層。
  • invalidate() 只能在 UI 執行緒呼叫,背景執行緒要換成 postInvalidate(),這也是為什麼上面準備了兩個 setProgress。
  • 進度點的旋轉動畫 duration 設成 1 毫秒、搭配 setFillAfter(true),效果等於「直接跳到目標角度」,用來讓標註點跟著進度走。

接著只要把它放進版面配置,指定大小並置中對齊:

<org.nsg.main.CircleProgressBar
    android:id="@+id/sync_progress"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerHorizontal="true" />

剩下的頂部標題列、提醒文字與「更新」按鈕都是常規寫法,這裡不展開。定義好這個 View,最難的部分其實就過去了;把動畫接上之後,百分比會依設計好的進度變化。

結語

雲通訊錄這個系列我會慢慢分享,專案也放在下面供大家下載學習:https://files.cnblogs.com/Alandre/AndroidUI04.rar

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