編輯:關於 Android 程式設計
QQ 健康中的運動介面具有圓弧進度、步數排名、好友平均步數與底部波紋等元素。這類介面可以通過自定義 View 完成,既能展示數據,也能呈現豐富的視覺效果。
<resources>
<attr name="titleColor" format="color" />
<attr name="lineColor" format="color" />
<declare-styleable name="MyQQHealthView">
<attr name="titleColor" />
<attr name="lineColor" />
</declare-styleable>
</resources>
TypedArray array = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.MyQQHealthView, defStyleAttr, 0);
int n = array.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = array.getIndex(i);
if (attr == R.styleable.MyQQHealthView_titleColor) {
textColor = array.getColor(attr, Color.BLACK);
} else if (attr == R.styleable.MyQQHealthView_lineColor) {
lineColor = array.getColor(attr, Color.BLACK);
}
}
array.recycle();
圓弧部分可以使用 drawArc 完成。先確定圓弧所在的外接矩形,再設置起始角度與掃描角度。
arcPaint.setStrokeWidth(widthBg / 20);
arcPaint.setStyle(Paint.Style.STROKE);
arcPaint.setColor(lineColor);
RectF arcRect = new RectF(widthBg * 1 / 4, widthBg * 1 / 4,
widthBg * 3 / 4, widthBg * 3 / 4);
canvas.drawArc(arcRect, 120, 300, false, arcPaint);
arcPaint.setColor(textColor);
canvas.drawArc(arcRect, 120, arcNum, false, arcPaint);
文字部分可以根據步數、排名與平均步數進行定位。虛線可以使用 DashPathEffect。
DashPathEffect effects = new DashPathEffect(new float[]{5, 5}, 1);
linePaint.setPathEffect(effects);
canvas.drawPath(linePath, linePaint);
下方的豎線可以根據每天步數動態計算高度。
可以使用 ValueAnimator 讓步數、排名與圓弧角度從 0 逐漸過渡到目標值。
ValueAnimator walkAnimator = ValueAnimator.ofInt(0, mySize);
walkAnimator.addUpdateListener(animation -> {
walkNum = (int) animation.getAnimatedValue();
postInvalidate();
});
ValueAnimator arcAnimator = ValueAnimator.ofFloat(0, arcTarget);
arcAnimator.addUpdateListener(animation -> {
arcNum = (float) animation.getAnimatedValue();
postInvalidate();
});
AnimatorSet animSet = new AnimatorSet();
animSet.setDuration(3000);
animSet.playTogether(walkAnimator, arcAnimator);
animSet.start();
自定義 View 的關鍵在於先理清層次與坐標。建議在繪製前先在紙上畫出大致結構,標注各部分的起點與比例,再逐步用 Canvas 實現。這樣可以讓代碼更清晰,也更容易維護。
Android Studio 使用 AndroidJUnitRunner 進行單元測試
Google 目前推薦使用 AndroidJUnitRunner 作為測試執行器。本文記錄在 Android Studio 中把它跑起來的完整設定流程。 一、build.
Android 記事本專案:圖片等比例縮放與為圖片加上邊框
在 Android 的 UI 開發中,圖片縮放是很常見的需求。以記事本為例,現在的圖片普遍尺寸較大,若將原圖未經縮放直接放進畫面,不僅會佔滿整個螢幕,甚至可能比螢幕還大,
自定義 View 之 ProgressBar:測量、繪制與自定義屬性一次講透
這次要動手做一個圓形的 ProgressBar。順便把自定義 View 裡兩個繞不開的知識點講清楚:View 的測量與自定義屬性。 先看效果: 一、準備工作:先想清
Android 生成 keystore 的兩種方式:Eclipse 圖形化與命令行
發布前必須準備好簽名文件。這裡整理兩條路徑:一條是在 Eclipse 裡用向導點完就好,另一條是用 keytool 命令直接生成。 一、在 Eclipse 中生成 先打開