編輯:關於 Android 開發
最近使用印象筆記,覺得 Android 版的底部導航欄相當出色,許多應用都採用類似設計,於是動手實作了一個。先來看效果:

要完成這樣的效果,需要自定義一個 ViewGroup,主要包含三個步驟:
1、onMeasure(測量過程)
2、onLayout(布局)
3、添加動畫
onMeasure(測量過程)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View viewChild = getChildAt(i);
// 對每個子 View 進行測量
measureChild(viewChild, widthMeasureSpec, heightMeasureSpec);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
onLayout(布局)
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
View btn_main = getChildAt(getChildCount() - 1);
int left = 0;
int top = 0;
if (changed) {
for (int i = getChildCount() - 1; i >= 0; i--) {
View viewChild = getChildAt(i);
int width = viewChild.getMeasuredWidth();
int height = viewChild.getMeasuredHeight();
left = getMeasuredWidth() - width;
top = getMeasuredHeight() - height - distance;
viewChild.layout(left, top, getMeasuredWidth(), getMeasuredHeight());
distance += getDisension(100);
}
btn_main.setOnClickListener(this);
changeState(currentState);
}
}
添加動畫
public void openTranslateAnimation(View view) {
distance = 0;
RotateAnimation animation = new RotateAnimation(0f, 45f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(500);
animation.setFillAfter(true);
view.startAnimation(animation);
for (int i = getChildCount() - 2; i >= 0; i--) {
View childView = getChildAt(i);
view.setVisibility(View.VISIBLE);
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0.35f, Animation.RELATIVE_TO_SELF, 0);
translate.setDuration(100);
translate.setStartOffset(10 * offset);
translate.setFillAfter(true);
childView.startAnimation(translate);
offset++;
}
}
public void closeTranslateAnimation(View view) {
distance = 0;
RotateAnimation animation = new RotateAnimation(45f, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(500);
animation.setFillAfter(true);
view.startAnimation(animation);
for (int i = getChildCount() - 2; i >= 0; i--) {
View childView = getChildAt(i);
view.setVisibility(View.VISIBLE);
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.35f);
translate.setDuration(100);
translate.setStartOffset(50 * offset);
childView.startAnimation(translate);
offset++;
}
}
主佈局如下:
<com.xby.fm.view.stackmenu
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingRight="10dp" android:text="首頁" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@mipmap/ic_menu_item" />
</LinearLayout>
<LinearLayout ...>...我的音樂...</LinearLayout>
<LinearLayout ...>...我的收藏...</LinearLayout>
<LinearLayout ...>...個人中心...</LinearLayout>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@mipmap/ic_main_menu" />
</com.xby.fm.view.stackmenu>
結構相對簡單,感興趣的讀者可搜尋 StackMenu 專案取得完整原始碼。
Android Studio 開發環境建置與 Android 專案建立指南
Android Studio 開發環境建置與 Android 專案建立 本文以 Mac 環境為例,整理 Android 開發的初始設定流程。整體概念與 Windows 類
微信支付只能成功調用一次的問題排查與解決
問題現象 在接入微信支付時,開發者可能會遇到這樣的情況:第一次調用支付可以正常打開微信並展示訂單信息,但取消支付後再次進入,卻無法再調起微信支付。即使更換訂單號,問題仍然
Android 手機衛士:綁定 SIM 卡序號
綁定 SIM 卡序號 手機防盜功能中,可透過綁定 SIM 卡序號判斷卡片是否被更換。實作時需先加入讀取電話狀態權限,再使用 TelephonyManager 取得序號。
方形進度條範例 App 拆解:側滑選單、自定義 View 與彈出視窗的組合技
這個範例以開源的 android-square-progressbar 為核心,示範如何把一個方形進度條做成可以「玩」的展示頁:左側抽屜選單列出可調參數,右側畫面即時反