編輯:關於 Android 程式設計
作者:泥沙磚瓦漿木匠 網站:https://blog.csdn.net/jeffli1993
個人簽名:打算起手不凡寫出鴻篇巨作的人,往往堅持不了完成第一章節。
交流 QQ 群:【編程之美 365234583】
繼續 Android UI 系列。UI 說到底是一門前端的活,美感才是最終的 boss 階段。這一輪也想順便把 Android 的一些簡單入門寫下來,鞏固提升一下觀念。
有道是「路漫漫其修遠兮,吾將上下而求索」。任何東西都不是一步登天,注意細節、一步一步來,自然就值得了。但也請記住:做人做事第一,技術第二。
先把結構拆開:
要做這個,得先熟悉 Canvas 與 Paint 的相關知識。Android 的 2D 繪圖 API 都在 android.graphics 與 android.graphics.drawable 兩個套件裡:圖形相關的有 Point、RectF 等,動畫相關的有 AnimationDrawable、BitmapDrawable、TransitionDrawable 等。
而這個範例用到的其實只有兩個動作:定義一個矩形區域(RectF),然後在這個區域裡畫兩段弧線——一段白色的是進度條的底,一段藍色的是實際進度。角度算好,剩下的自然就出來了。
public class CircleProgressBar extends View {
private int maxProgress = 100;
private int progress = 15;
private int progressStrokeWidth = 16;
private int marxArcStorkeWidth = 16;
RectF oval; // 畫圓所在的矩形區域
Paint paint;
public CircleProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
oval = new RectF();
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = this.getWidth();
int height = this.getHeight();
width = (width > height) ? height : width; // 取短邊,確保是正圓
height = (width > height) ? height : width;
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
canvas.drawColor(Color.TRANSPARENT);
paint.setStrokeWidth(progressStrokeWidth);
paint.setStyle(Style.STROKE);
// 依線寬內縮,避免圓被畫布切掉
oval.left = marxArcStorkeWidth / 2;
oval.top = marxArcStorkeWidth / 2;
oval.right = width - marxArcStorkeWidth / 2;
oval.bottom = height - marxArcStorkeWidth / 2;
canvas.drawArc(oval, -90, 360, false, paint); // 白色底圈
paint.setColor(Color.rgb(0x57, 0x87, 0xb6));
paint.setStrokeWidth(marxArcStorkeWidth);
canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 進度弧
paint.setStrokeWidth(1);
String text = progress + "%";
int textHeight = height / 4;
paint.setTextSize(textHeight);
int textWidth = (int) paint.measureText(text, 0, text.length());
paint.setStyle(Style.FILL);
canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, paint);
}
/** 設置進度,僅限 UI 執行緒呼叫 */
public void setProgress(int progress, View view) {
this.progress = progress;
view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress)));
this.invalidate();
}
/** 非 UI 執行緒呼叫 */
public void setProgressNotInUiThread(int progress, View view) {
this.progress = progress;
view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress)));
this.postInvalidate();
}
/** 進度標註點的旋轉動畫(圖片本身偏移約 54 度,起始角度要補回來) */
private Animation pointRotationAnima(float fromDegrees, float toDegrees) {
int initDegress = 300;
RotateAnimation animation = new RotateAnimation(fromDegrees, initDegress + toDegrees,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1);
animation.setRepeatCount(1);
animation.setFillAfter(true);
return animation;
}
}
幾個實作重點:
接著只要把它放進版面配置,指定大小並置中對齊:
<org.nsg.main.CircleProgressBar
android:id="@+id/sync_progress"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true" />
剩下的頂部標題列、提醒文字與「更新」按鈕都是常規寫法,這裡不展開。定義好這個 View,最難的部分其實就過去了;把動畫接上之後,百分比會依設計好的進度變化。
雲通訊錄這個系列我會慢慢分享,專案也放在下面供大家下載學習:https://files.cnblogs.com/Alandre/AndroidUI04.rar
Android 顏色資源使用完全指南
前言在 Android 開發中,顏色資源可分為兩種:一種是自行定義的顏色,另一種是使用系統內建的顏色。自訂顏色Android 的顏色值由透明度 alpha 與 RGB(紅
高德地圖怎麼看街景?手機版三步驟教學
想先「走一遍」還沒去過的城市,街景服務是最方便的辦法。手機版高德地圖裡就藏著這個功能,操作相當簡單。 第一步:點開右上方的功能按鈕 開啟手機版【高德地圖】,點擊螢幕右上
Buzz 桌面教學:在桌面加入文字
有些使用者想在 Buzz 桌面上添點小浪漫、小驚喜或一點文藝氣息,這時候在桌面放幾行字是最直接的做法。Buzz 桌面支援很方便的文字小工具,寫上「Honey, I lov
Android 藍牙架構解析(二):從 UART 到 RFCOMM 的數據流
Android 藍牙架構概覽Android 藍牙系統通常可分成核心驅動、BlueZ 協議棧與上層 Java API。BlueZ 負責處理藍牙協議與裝置管理,是理解 And