Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

仿印象筆記底部導航欄:自定義 ViewGroup 實現弧形展開選單

編輯:關於 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 專案取得完整原始碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved