編輯:關於 Android 程式設計

靜態圖:

動態圖:

這個圖表本質是七層同心正七邊形,加上頂點標籤與依資料填充的內容區域。拆成步驟如下:
核心只有兩件事:座標的計算(三角函數的應用)與旋轉角度的掌控。一旦把七等分的 360°/7 想通,剩下的都是重複勞動。
/**
* 繪製七邊形中心線
*/
private void center(Canvas canvas) {
canvas.save(); // 保存目前狀態
canvas.rotate(0, center, center);
float startY = getPaddingTop() + 2 * str_rect.height();
float endY = center;
float du = (float) (360 / 7 + 0.5); // 七等分,加 0.5 補償誤差
for (int i = 0; i < 7; i++) {
canvas.drawLine(center, startY, center, endY, center_paint);
canvas.rotate(du, center, center);
}
canvas.restore(); // 恢復之前狀態
}

做法是先算出圓心到 A 點的座標,確定旋轉角度為 360/7,然後讓 Canvas 繞圓心旋轉七次,每次畫一條線。為了方便肉眼驗證,先鋪一層綠色背景作為參考。


七個頂點座標全部靠三角函數推導。
A 點(正上方)
x = center; y = getPaddingTop() + 2 * str_rect.height();
B 點(右上)

// OF = cos(360/7 + 360/7/2) * one_radius // FC = sin(360/7 + 360/7/2) * one_radius float x = center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * one_radius; float y = Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * one_radius + center;
D 點(右側偏下)

// HD = sin(360/7/2) * one_radius // OH = cos(360/7/2) * one_radius float x = center + Math.sin(Math.toRadians(360 / 7 / 2)) * one_radius; float y = Math.cos(Math.toRadians(360 / 7 / 2)) * one_radius + center;
右半邊算完,左半邊就簡單了:y 座標完全相同,x 座標只要把加號改成減號即可。

最外層完成後,剩下的就好辦了——只要算出每層的間距。從原型圖分析,相鄰兩層的間距為外接圓半徑的四分之一,因此各層的「有效半徑」分別是 one_radius - 1/4R、- 2/4R、- 3/4R。
/** 第二層 */
private void twoPolygons(Canvas canvas) {
distance = one_radius / 4;
Path path = new Path();
path.moveTo(center, getPaddingTop() + 2 * str_rect.height() + distance);
path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7)) * (one_radius - distance)),
(float) (getPaddingTop() + 2 * str_rect.height() + one_radius
- Math.abs(Math.cos(Math.toRadians(360 / 7)) * (one_radius - distance))));
path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (one_radius - distance)),
(float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (one_radius - distance)) + center);
path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 / 2)) * (one_radius - distance)),
(float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (one_radius - distance)) + center);
path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 / 2)) * (one_radius - distance)),
(float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (one_radius - distance)) + center);
path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (one_radius - distance)),
(float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (one_radius - distance)) + center);
path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7)) * (one_radius - distance)),
(float) (getPaddingTop() + 2 * str_rect.height() + one_radius
- Math.abs(Math.cos(Math.toRadians(360 / 7)) * (one_radius - distance))));
path.close();
canvas.drawPath(path, two_paint);
}
/** 第三層:只需把 distance 換成 one_radius / 2 */
private void threePolygons(Canvas canvas) {
distance = one_radius / 2;
// 其餘 path 計算與 twoPolygons 完全相同
}
/** 最內層:distance 換成 one_radius / 2 + one_radius / 4 */
private void fourPolygons(Canvas canvas) {
distance = one_radius / 2 + one_radius / 4;
// 其餘 path 計算與 twoPolygons 完全相同
}
三個方法的差異只有 distance 一個變數,實作時可以抽成一個帶參數的公共方法。移除綠色參考背景後:

文字位置以最外層各頂點座標為基準,再按文字本身的寬高做微調,讓視覺上落在對齊的位置。
private Rect str_rect; // 文字實際邊距
String[] str = {"擊殺", "生存", "助攻", "物理", "魔法", "防御", "金錢"};
private void PaintFout(Canvas canvas) {
canvas.drawText(str[0], center - str_rect.width() / 2,
(float) (getPaddingTop() + 1.5 * str_rect.height()), str_paint);
canvas.drawText(str[1],
(float) (center + Math.sin(Math.toRadians(360 / 7)) * one_radius + str_rect.height() / 2),
(float) (getPaddingTop() + 2 * str_rect.height() + one_radius
- Math.abs(Math.cos(Math.toRadians(360 / 7)) * one_radius)), str_paint);
canvas.drawText(str[2],
(float) (center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * one_radius + str_rect.height() / 2),
(float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * one_radius) + center + str_rect.height() / 2,
str_paint);
canvas.drawText(str[3],
(float) (center + Math.sin(Math.toRadians(360 / 7 / 2)) * one_radius - str_rect.height() / 2 + str_rect.width() / 2),
(float) ((Math.cos(Math.toRadians(360 / 7 / 2)) * one_radius) + center + str_rect.height()), str_paint);
canvas.drawText(str[4],
(float) (center - Math.sin(Math.toRadians(360 / 7 / 2)) * one_radius + str_rect.height() / 2 - str_rect.width() * 1.5),
(float) ((Math.cos(Math.toRadians(360 / 7 / 2)) * one_radius) + center + str_rect.height()), str_paint);
canvas.drawText(str[5],
(float) (center - Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * one_radius - str_rect.height() / 2 - str_rect.width()),
(float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * one_radius) + center + str_rect.height() / 2,
str_paint);
canvas.drawText(str[6],
(float) (center - Math.sin(Math.toRadians(360 / 7)) * one_radius - str_rect.height() / 2 - str_rect.width()),
(float) (getPaddingTop() + 2 * str_rect.height() + one_radius
- Math.abs(Math.cos(Math.toRadians(360 / 7)) * one_radius)), str_paint);
}

這一步是整個控件的重點,也是它「活起來」的地方。做法與繪製多邊形完全相同,只是每個頂點各自使用不同的縮減量(f1 ~ f7),由外部傳入的七項數值換算而來。
/**
* 繪製能力值填充區域
*/
private void PaintRank(Canvas canvas) {
Path path = new Path();
path.moveTo(center, getPaddingTop() + 2 * str_rect.height() + f1);
path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7)) * (one_radius - f2)),
(float) (getPaddingTop() + 2 * str_rect.height() + one_radius
- Math.abs(Math.cos(Math.toRadians(360 / 7)) * (one_radius - f2))));
path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (one_radius - f3)),
(float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (one_radius - f3)) + center);
path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 / 2)) * (one_radius - f4)),
(float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (one_radius - f4)) + center);
path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 / 2)) * (one_radius - f5)),
(float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (one_radius - f5)) + center);
path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (one_radius - f6)),
(float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (one_radius - f6)) + center);
path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7)) * (one_radius - f7)),
(float) (getPaddingTop() + 2 * str_rect.height() + one_radius
- Math.abs(Math.cos(Math.toRadians(360 / 7)) * (one_radius - f7))));
path.close();
canvas.drawPath(path, mPaint);
}

回頭看,整個自定義 View 並沒有什麼魔法:七等分角度、三角函數求頂點、Path 連點成面。真正的功夫花在把重複的座標計算整理清楚,讓程式碼可以被讀懂。
若覺得有幫助,歡迎到 GitHub 下載完整源碼,也歡迎給個 star 支持:
https://github.com/jiangzehui/polygonsview
後續可以再優化的方向還不少:把座標計算抽成獨立工具方法、支援任意邊數、加入動畫插值讓填充區域平滑生長,以及自定義各頂點顏色。
ListView 中為不同按鈕綁定各自的點擊事件
當 ListView 的每一項裡有兩個以上的按鈕時,直接為它們各寫一個 OnClickListener 很快就會亂掉。比較乾淨的做法是:在 Adapter 內部綁定點擊,
Android 取得所有已安裝應用資訊:PackageManager 掃描與 SharedPreferences 保存
若應用需要列出裝置中的已安裝程式,通常會使用 PackageManager。透過它,可以取得應用名稱、圖示、套件名與啟動 Activity 等資訊,並進一步做分類或快速啟
Android 開發自學筆記(三):APP 布局入門上篇
大家好,本文將帶你踏出開發一款美觀、有質感、人見人愛的 Android 應用程式的第一步。市面上有不少布局精美、在視覺上令人愛不釋手的應用,如果換作我們來著手開發,該從何
深入 Android 視圖源碼:Window、PhoneWindow 與 Activity 的關係全解析
寫 Android 界面久了,總會遇到一些「知其然不知其所以然」的問題:為什麼 requestWindowFeature() 必須寫在 setContentView()