編輯:關於 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 兩種建立方式。
@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、二維變換、多層繪製這些進階主題會順暢很多。
Android 實作支付寶咻一咻漣漪效果的兩種思路
支付寶咻一咻以漣漪與呼吸動畫營造熱鬧感。Android 可透過自訂 View 或屬性動畫實作類似效果,本文將比較兩種思路,說明各自的實作重點。 自訂 View 繪製漣漪
深入 Android 線程池:ScheduledThreadPoolExecutor 源碼剖析
ScheduledThreadPoolExecutor 是一個支持定時任務與周期任務的線程池。如果讀過上一篇 Android Java 線程池 ThreadPoolExe
Android APK 在 32 位元與 64 位元系統下的差異解析
如今 64 位元 Android 系統日益普及,越來越多 APK 宣稱支援 64 位元,於是在打包時加入 arm64-v8a 目錄並放入 64 位元的 so。但若程式碼未
Android 自訂 Dialog 對話框實例:從基礎版到炫酷升級版
在使用 Android SDK 提供的 AlertDialog 時,常常會因為系統版本或第三方 ROM(例如 MIUI)的差異而出現不一致的外觀效果。為了獲得統一且可控的