編輯:關於 Android 程式設計
支付寶裡的芝麻信用分,用一張五維雷達圖把「履約能力、信用歷史、人脈關系、行為偏好、身份特質」攤在同一個平面上,中間還壓著一個大大的分數。資訊密度高、辨識度強,很值得自己動手做一個。

把畫面拆開來看,其實只有六件事:
難點幾乎全在第 3、5、6 步——本質上都是三角函數加上一點座標微調。
在 onSizeChanged 裡,依 View 實際長寬算出半徑與中心點。這裡取「寬高較小值的一半再減半」,也就是直徑佔短邊的一半,留白比較舒服;這個比例完全可以做成自定義屬性。
public class CreditScoreView extends View {
private int dataCount = 5; // 維度數量
private float radian = (float) (Math.PI * 2 / dataCount); // 相鄰兩點的夾角
private float radius; // 雷達圖半徑
private int centerX;
private int centerY;
private String[] titles = {"履約能力", "信用歷史", "人脈關系", "行為偏好", "身份特質"};
private int[] icons = {R.mipmap.ic_performance, R.mipmap.ic_history,
R.mipmap.ic_contacts, R.mipmap.ic_predilection, R.mipmap.ic_identity};
private float[] data = {170, 180, 160, 170, 180};
private float maxValue = 190; // 單維度滿分
private int radarMargin = DensityUtils.dp2px(getContext(), 15); // 圖與標題間距
private int scoreSize = DensityUtils.dp2px(getContext(), 28);
private int titleSize = DensityUtils.dp2px(getContext(), 13);
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
radius = Math.min(h, w) / 2 * 0.5f;
centerX = w / 2;
centerY = h / 2;
postInvalidate();
}
}
關鍵在 getPoint 這個方法——它把「第幾個頂點」換算成實際座標,後面所有繪製都靠它。
private void drawPolygon(Canvas canvas) {
Path path = new Path();
for (int i = 0; i < dataCount; i++) {
Point p = getPoint(i);
if (i == 0) {
path.moveTo(p.x, p.y);
} else {
path.lineTo(p.x, p.y);
}
}
path.close();
canvas.drawPath(path, mainPaint);
}
private void drawLines(Canvas canvas) {
Path path = new Path();
for (int i = 0; i < dataCount; i++) {
path.reset();
path.moveTo(centerX, centerY);
path.lineTo(getPoint(i).x, getPoint(i).y);
canvas.drawPath(path, mainPaint);
}
}
getPoint 有兩個版本。單參數版本用於背景網格與連接線(間距 0、比例 1);三參數版本多帶了 radarMargin 與 percent,專門服務標題定位與覆蓋區域。
private Point getPoint(int position) {
return getPoint(position, 0, 1);
}
private Point getPoint(int position, int radarMargin, float percent) {
int x = 0, y = 0;
if (position == 0) { // 正上方
x = (int) (centerX + (radius + radarMargin) * Math.sin(radian) * percent);
y = (int) (centerY - (radius + radarMargin) * Math.cos(radian) * percent);
} else if (position == 1) { // 右上
x = (int) (centerX + (radius + radarMargin) * Math.sin(radian / 2) * percent);
y = (int) (centerY + (radius + radarMargin) * Math.cos(radian / 2) * percent);
} else if (position == 2) { // 右下
x = (int) (centerX - (radius + radarMargin) * Math.sin(radian / 2) * percent);
y = (int) (centerY + (radius + radarMargin) * Math.cos(radian / 2) * percent);
} else if (position == 3) { // 正下方
x = (int) (centerX - (radius + radarMargin) * Math.sin(radian) * percent);
y = (int) (centerY - (radius + radarMargin) * Math.cos(radian) * percent);
} else if (position == 4) { // 左上
x = centerX;
y = (int) (centerY - (radius + radarMargin) * percent);
}
return new Point(x, y);
}
這段程式碼是寫死的五維版本。若要支援任意維度,可以改成用迴圈加 sin/cos 直接算:以正上方為起點,第 i 個頂點的角度就是 i × radian,座標為 centerX + r·sin(θ)、centerY − r·cos(θ)。這樣一来 dataCount 就能做成自定義屬性了。

每個維度的分值除以滿分得到百分比,再帶進 getPoint 縮放半徑,就得到覆蓋區域的每個頂點。這裡有個小技巧:先用 STROKE 畫一次描邊,再用 FILL_AND_STROKE 畫一次,邊界會更銳利。
private void drawRegion(Canvas canvas) {
Path path = new Path();
for (int i = 0; i < dataCount; i++) {
float percent = data[i] / maxValue;
Point p = getPoint(i, 0, percent);
if (i == 0) {
path.moveTo(p.x, p.y);
} else {
path.lineTo(p.x, p.y);
}
}
path.close();
valuePaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, valuePaint);
valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawPath(path, valuePaint);
}

把各維度分值加總,畫在中心點上。這裡要注意文字基線——drawText 的 y 參數指的是基線位置,所以要往上補半個字高,視覺上才真的居中。
private void drawScore(Canvas canvas) {
int score = 0;
for (int i = 0; i < dataCount; i++) {
score += data[i];
}
canvas.drawText(score + "", centerX, centerY + scoreSize / 2, scorePaint);
}

標題的座標取自 getPoint(i, radarMargin, 1),也就是往外推了 radarMargin 之後的頂點。但這只是個基準點,五個方向各自的對齊方式都不同,需要逐一微調:
private void drawTitle(Canvas canvas) {
for (int i = 0; i < dataCount; i++) {
Point p = getPoint(i, radarMargin, 1);
int x = p.x;
int y = p.y;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), icons[i]);
int iconHeight = bitmap.getHeight();
float titleWidth = titlePaint.measureText(titles[i]);
if (i == 1) { // 右上:整段往下挪半個圖示高
y += iconHeight / 2;
} else if (i == 2) { // 右下:往左退一個標題寬,同時下移
x -= titleWidth;
y += iconHeight / 2;
} else if (i == 3) { // 正下:往左退一個標題寬
x -= titleWidth;
} else if (i == 4) { // 左上:往左退半個標題寬(居中)
x -= titleWidth / 2;
}
// i == 0(正上方)預設即為居中,不需調整
canvas.drawText(titles[i], x, y, titlePaint);
}
}

圖示要放在標題上方並水平居中,所以基準是「標題左下角座標」,再往上推一個圖示高度加一個文字高度。
private void drawIcon(Canvas canvas) {
for (int i = 0; i < dataCount; i++) {
Point p = getPoint(i, radarMargin, 1);
int x = p.x;
int y = p.y;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), icons[i]);
int iconWidth = bitmap.getWidth();
int iconHeight = bitmap.getHeight();
float titleWidth = titlePaint.measureText(titles[i]);
int textHeight = getTextHeight(titlePaint);
if (i == 0) {
x += (titleWidth - iconWidth) / 2;
y -= (iconHeight + textHeight);
} else if (i == 1) {
x += (titleWidth - iconWidth) / 2;
y -= (iconHeight / 2 + textHeight);
} else if (i == 2) {
x -= (iconWidth + (titleWidth - iconWidth) / 2);
y -= (iconHeight / 2 + textHeight);
} else if (i == 3) {
x -= (iconWidth + (titleWidth - iconWidth) / 2);
y -= (iconHeight + textHeight);
} else if (i == 4) {
x -= iconWidth / 2;
y -= (iconHeight + textHeight);
}
canvas.drawBitmap(bitmap, x, y, titlePaint);
}
}
private int getTextHeight(Paint paint) {
Paint.FontMetrics fm = paint.getFontMetrics();
return (int) (fm.descent - fm.ascent);
}

目前的版本是靜態繪製。若想让覆蓋區域有「長出來」的效果,最簡單的做法是加一個 mAnimPercent 成員,用 ValueAnimator 從 0 動到 1,在 drawRegion 裡把 percent = data[i] / maxValue * mAnimPercent,每帧呼叫 invalidate 即可。
另外有兩處值得優化:
最後,記得把 radius、radarMargin、分值顏色、網格線數等抽成自定義屬性(attrs.xml + obtainStyledAttributes),這個控件才算真正完成。
設計模式:命令模式(Command Pattern)詳解
命令模式(Command Pattern):將請求封裝成物件,以便使用不同的請求、佇列或日誌來參數化其他物件。命令模式也可以用來支援撤銷操作。 以下以「遙控器控制電燈與車
仿 QQ 分組管理:可拖動排序的 ListView 完整實現
項目裡需要實現分組管理,其中一項功能是列表項可長按拖動排序。查資料並參考了一些現有實現之後把功能跑了起來,這裡整理代碼與思路,方便複盤。 整體思路 核心是重寫 ListV
XMPP 群組實作指南:從建立 Room JID 到設定群組參數
在公司專案中實作自有 IM SDK 時,群聊是常見需求。本文整理 XMPP 建立群組的流程,重點放在 XEP-0045 的 Multi-User Chat 概念,以及如何
Android 異步更新 UI 實戰:線程池 + Future + Handler 三者搭配
前言 在開發 Android 的過程中,處理耗時任務與 UI 交互時,我們都會把耗時任務放到子執行緒處理再刷新畫面。下面兩個問題,相信大多數開發者都碰過: 資料經常