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

Android 自定義 View 實作:仿滴滴出行「十大司機評選」活動說明頁

編輯:關於 Android 程式設計

前言

最近用滴滴出行的時候,注意到一個很有意思的 View 效果。看到心動的介面總會手癢想自己畫一遍,這篇就大致模仿一下這個自定義 View 的畫法。如果有更好的實現思路,歡迎留言指導。

對照一下原版與仿的效果:

步驟一:拆解需要的變數

先想清楚要畫什麼,再決定需要哪些參數。這個效果本質上是「兩排圓點 + 左右兩個三角形」的裝飾性背景。

// 圓的半徑
private int radius = 8;
// 圓之間的間距
private int gap = 8;
private Paint mPaint;
// 返回字體的高度
private float textViewHeight = dp2px(55, getContext());
// 三角形的寬度
private float triAngleWidth = 60;

dp 與 px 的換算方法照舊:

public static int dp2px(float dp, Context ctx) {
    float density = ctx.getResources().getDisplayMetrics().density;
    int px = (int) (dp * density + 0.5f);  // 加 0.5 可以四捨五入
    return px;
}

字體高度 55dp 怎麼來的?底部那個「返回」TextView 的上下 padding 各 15dp(合計 30dp),加上 TextSize 的 25sp,加總就是 55dp。這個高度用來定位中間那一排圓。

三角形寬度怎麼理解?以左三角形為例,圖中標出的 1、2、3 三處,用的都是這一個值。

步驟二:初始化畫筆

public MyCardView(Context context) {
    super(context);
    init();
}

public MyCardView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyCardView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    mPaint = new Paint();
    mPaint.setColor(Color.WHITE);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setDither(true);   // 開啟抖動,避免漸變處出現色帶
}

三個構造方法都要覆寫,是為了兼容代碼中 new 與 XML inflate 兩種建立方式。

步驟三:在 onDraw 中完成繪製

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // 畫上下兩排圓
    int roundNum = getWidth() / (radius * 2 + gap * 2);
    for (int i = 1; i <= roundNum; i++) {
        canvas.drawCircle((gap + radius) * (2 * i - 1), 0, radius, mPaint);
        canvas.drawCircle((gap + radius) * (2 * i - 1), getHeight(), radius, mPaint);
    }

    // 畫中間那排圓:可用寬度要扣掉兩側三角形的寬度
    int roundNum2 = (int) ((getWidth() - (2 * triAngleWidth)) / (radius * 2 + gap * 2));
    for (int i = 1; i <= roundNum2; i++) {
        canvas.drawCircle((gap + radius) * (2 * i - 1) + triAngleWidth,
                getHeight() - textViewHeight, radius, mPaint);
    }

    // 畫左三角形
    Path path = new Path();
    path.moveTo(0, getHeight() - textViewHeight - triAngleWidth / 2);
    path.lineTo(0, getHeight() - textViewHeight + triAngleWidth / 2);
    path.lineTo(triAngleWidth, getHeight() - textViewHeight);
    path.close();
    canvas.drawPath(path, mPaint);

    // 畫右三角形
    path.reset();
    path.moveTo(getWidth(), getHeight() - textViewHeight - triAngleWidth / 2);
    path.lineTo(getWidth(), getHeight() - textViewHeight + triAngleWidth / 2);
    path.lineTo(getWidth() - triAngleWidth, getHeight() - textViewHeight);
    path.close();
    canvas.drawPath(path, mPaint);
}

步驟四:在佈局檔中使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:padding="60dp">

    <com.handsome.app2.view.custom.MyCardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#D0C0A3"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="15dp"
                android:text="Hensen博客有獎競猜\n活動獎品說明"
                android:textColor="#7F3912"
                android:textSize="18sp" />
        </LinearLayout>

        <!-- 中間的主體內容 -->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="15dp"
                android:text="返回"
                android:textColor="#7F3912"
                android:textSize="22sp" />
        </LinearLayout>
    </com.handsome.app2.view.custom.MyCardView>
</RelativeLayout>

步驟五:回顧繪製思路

1. 上下兩排圓。間距公式是 radius * 2 + gap * 2,用總寬度除以單個週期就得到個數;圓心 x 座標用 (gap + radius) * (2 * i - 1) 遞推。y 座標分別取 0 和 getHeight(),正好一半露在邊界外,形成半圓的效果。

2. 中間那排圓。個數算法和上面一樣,只是分子要減掉左右兩個三角形的寬度 2 * triAngleWidth,起始位置也要相應地加上 triAngleWidth 的偏移;y 座標則是 getHeight() - textViewHeight。

3. 兩個三角形。左三角形:把 Path 移到 A 點,lineTo 到 B 點,再 lineTo 到 C 點,最後 close() 自動從 C 連回 A。右三角形同理,只是 x 座標換成 getWidth() 並往內收。

完整源碼

public class MyCardView extends LinearLayout {

    private int radius = 8;                                        // 圓的半徑
    private int gap = 8;                                           // 圓之間的間距
    private Paint mPaint;
    private float textViewHeight = dp2px(55, getContext());        // 返回字體高度
    private float triAngleWidth = 60;                              // 三角形寬度

    public MyCardView(Context context) {
        super(context);
        init();
    }

    public MyCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyCardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setDither(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 畫上下圓
        int roundNum = getWidth() / (radius * 2 + gap * 2);
        for (int i = 1; i <= roundNum; i++) {
            canvas.drawCircle((gap + radius) * (2 * i - 1), 0, radius, mPaint);
            canvas.drawCircle((gap + radius) * (2 * i - 1), getHeight(), radius, mPaint);
        }

        // 畫中間的圓
        int roundNum2 = (int) ((getWidth() - (2 * triAngleWidth)) / (radius * 2 + gap * 2));
        for (int i = 1; i <= roundNum2; i++) {
            canvas.drawCircle((gap + radius) * (2 * i - 1) + triAngleWidth,
                    getHeight() - textViewHeight, radius, mPaint);
        }

        // 畫左三角形
        Path path = new Path();
        path.moveTo(0, getHeight() - textViewHeight - triAngleWidth / 2);
        path.lineTo(0, getHeight() - textViewHeight + triAngleWidth / 2);
        path.lineTo(triAngleWidth, getHeight() - textViewHeight);
        path.close();
        canvas.drawPath(path, mPaint);

        // 畫右三角形
        path.reset();
        path.moveTo(getWidth(), getHeight() - textViewHeight - triAngleWidth / 2);
        path.lineTo(getWidth(), getHeight() - textViewHeight + triAngleWidth / 2);
        path.lineTo(getWidth() - triAngleWidth, getHeight() - textViewHeight);
        path.close();
        canvas.drawPath(path, mPaint);
    }

    public static int dp2px(float dp, Context ctx) {
        float density = ctx.getResources().getDisplayMetrics().density;
        int px = (int) (dp * density + 0.5f);
        return px;
    }
}

一點心得

這個自定義 View 的難度並不高,真正的價值在於它示範了一種通用的分析套路:先把效果拆成若干個基本圖形,再把每個圖形的定位參數換算成「與控制項尺寸相關的表達式」。一旦參數全部相對化,控制項就能自適應任意寬高,而不是寫死一堆魔法數字。

沿著這個思路往下走,Path、二維變換、多層繪製這些進階主題會順暢很多。

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