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

Android 傚網易客戶端頂部導航欄:滑動指示條與左右捲動實作

編輯:關於 Android 程式設計

本文分享一個網易新聞風格的首頁頂部導航列動畫,並說明它的核心原理與實作細節。

版面結構

先來看布局。這份結構參考自網易客戶端的反編譯結果,理解之後不難改寫:

<FrameLayout android:id="@id/column_navi"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/top_column_bg">
    <HorizontalScrollView android:id="@id/column_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="45.0dip"
        android:layout_gravity="center"
        android:fadingEdge="vertical"
        android:paddingLeft="9.0dip"
        android:paddingRight="9.0dip"
        android:scrollbars="none">
        <FrameLayout android:layout_width="wrap_content"
            android:layout_height="fill_parent">
            <ImageView android:id="@id/column_slide_bar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:src="@drawable/slidebar" />
            <LinearLayout android:id="@id/column_title_layout"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:gravity="center_vertical"
                android:paddingLeft="5px"
                android:weightSum="6.0" />
        </FrameLayout>
    </HorizontalScrollView>
    <ImageButton android:id="@id/column_to_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="left|center"
        android:layout_marginLeft="2.0dip"
        android:layout_marginRight="1.0dip"
        android:background="#00000000"
        android:src="@drawable/arr_left"
        android:visibility="visible" />
    <ImageButton android:id="@id/column_to_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="right|center"
        android:layout_marginLeft="1.0dip"
        android:layout_marginRight="2.0dip"
        android:background="#00000000"
        android:src="@drawable/arr_right"
        android:visibility="visible" />
</FrameLayout>

幾個設計意圖值得說明:

· HorizontalScrollView:讓導航項超過一屏時可以左右滑動。

· 兩顆 ImageButton:負責觸發左右捲動。

· 內層的 FrameLayout:FrameLayout 是疊加式版面,所以 ImageView(滑動指示條)會被放在 LinearLayout 的下一層,正好作為動態背景;而 LinearLayout 裡放的是動態加入的 TextView。

指示條如何跟著移動

關鍵在於為每個 TextView 掛上 onTouch 事件並攔截 ACTION_DOWN(手指按下)。此時啟動一段 TranslateAnimation 平移動畫,並在動畫結束的當下真正更新 ImageView 的位置——做法是動態調整它的 leftMargin。

private void translateImage(MotionEvent event) {
    float x = event.getX();
    float rx = event.getRawX();
    final float nx = rx - x - 12;
    TranslateAnimation trans = null;
    if (nx > lastX) {
        trans = new TranslateAnimation(0, nx - lastX, 0, 0);
    } else if (nx < lastX) {
        trans = new TranslateAnimation(0, (lastX - nx) * -1, 0, 0);
    } else {
        return;
    }
    trans.setDuration(300);
    trans.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) { }

        @Override
        public void onAnimationRepeat(Animation animation) { }

        @Override
        public void onAnimationEnd(Animation animation) {
            // 動畫結束後才真正落點,避免回彈
            FrameLayout.LayoutParams params =
                    (android.widget.FrameLayout.LayoutParams) column_slide_bar.getLayoutParams();
            params.leftMargin = (int) nx;
            column_slide_bar.setLayoutParams(params);
        }
    });
    trans.setFillEnabled(true);
    column_slide_bar.startAnimation(trans);
    lastX = (int) nx;
}

開頭先取得手指按下處的座標,lastX 則是上一次按下的位置,兩相比較就能判斷移動方向。動畫結束後才把 ImageView 真正移到新座標,這樣視覺上才不會出現跳動。setFillEnabled(true) 的作用也是避免動畫結束瞬間的位置錯亂。

文字顏色的切換

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (up_text != null) {
            up_text.setTextColor(Color.BLACK);
        } else {
            TextView text = (TextView) context.findViewById(R.id.head_lines);
            text.setTextColor(Color.BLACK);
        }
        translateImage(event);
        TextView tv = (TextView) v;
        tv.setTextColor(Color.WHITE);
        up_text = tv;
    }
    return true;
}

邏輯很單純:先把上一個選中項還原成黑字,再把目前這個設為白字,並記下來作為下次的「上一個」。

左右按鈕控制捲動

private void addListener() {
    column_to_left.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            column_scrollview.smoothScrollTo(column_scrollview.getScrollX() - 40, 0);
        }
    });
    column_to_right.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            column_scrollview.smoothScrollTo(column_scrollview.getScrollX() + 40, 0);
        }
    });
}

動態建立導航文字

private void initView() {
    column_title_layout = (LinearLayout) findViewById(R.id.column_title_layout);
    column_scrollview = (HorizontalScrollView) findViewById(R.id.column_scrollview);
    column_slide_bar = (ImageView) findViewById(R.id.column_slide_bar);
    column_to_left = (ImageButton) findViewById(R.id.column_to_left);
    column_to_right = (ImageButton) findViewById(R.id.column_to_right);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(65, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
    params.leftMargin = 9;

    TextViewOnTouchListener listener = new TextViewOnTouchListener(column_slide_bar, this);
    TextView text = null;
    for (int i = 0; i < 6; i++) {
        text = new TextView(this);
        text.setTextSize(16);
        switch (i) {
            case 0:
                text.setId(R.id.head_lines);
                text.setTextColor(Color.WHITE);
                text.setText("頭條");
                break;
            case 1:
                text.setId(R.id.sport);
                text.setTextColor(Color.BLACK);
                text.setText("體育");
                break;
            case 2:
                text.setId(R.id.entertainment);
                text.setTextColor(Color.BLACK);
                text.setText("娛樂");
                break;
            case 3:
                text.setId(R.id.finance);
                text.setTextColor(Color.BLACK);
                text.setText("財經");
                break;
            case 4:
                text.setId(R.id.technology);
                text.setTextColor(Color.BLACK);
                text.setText("科技");
                break;
            case 5:
                text.setId(R.id.more);
                text.setTextColor(Color.BLACK);
                text.setText("更多");
                break;
        }
        text.setOnTouchListener(listener);
        column_title_layout.addView(text, params);
    }
}

動態元件需要的 id 宣告

由於 id 是在程式碼中指定,必須先在 res/values/ids.xml 宣告,否則編譯不會通過:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="column_scrollview" type="id"/>
    <item name="column_slide_bar" type="id"/>
    <item name="column_title_layout" type="id"/>
    <item name="column_navi" type="id"/>
    <item name="column_to_left" type="id"/>
    <item name="column_to_right" type="id"/>
    <item name="scroll_layout" type="id"/>
    <item name="vote" type="id"/>
    <item name="comment" type="id"/>
    <item name="picture" type="id"/>
    <item name="topic" type="id"/>
    <item name="news" type="id"/>
    <item name="head_lines" type="id"/>
    <item name="sport" type="id"/>
    <item name="entertainment" type="id"/>
    <item name="finance" type="id"/>
    <item name="technology" type="id"/>
    <item name="more" type="id"/>
</resources>

整體來說,這個效果的精髓只有兩句話:用 FrameLayout 的疊加特性讓指示條墊在文字下方,再用平移動畫加上「動畫結束後才落點」的寫法讓它平滑滑到目標位置。

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