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

Android Title 滑塊動畫實作:打造新聞客戶端的多欄目切換效果

編輯:關於 Android 程式設計

先來看效果:選擇不同模組時,滑塊會以動畫形式平滑滑過去,這種效果特別適合新聞客戶端展示眾多欄目。

佈局這樣寫:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >
    <include layout="@layout/title_layout" />
    <RelativeLayout android:id="@+id/column_navi"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/top_column_bg" >
        <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_marginLeft="5.0dp"
            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_marginRight="5.0dp"
            android:background="#00000000"
            android:src="@drawable/arr_right"
            android:visibility="visible" />
        <HorizontalScrollView android:id="@+id/column_scrollview"
            android:layout_width="fill_parent"
            android:layout_height="35.0dp"
            android:layout_toLeftOf="@+id/column_to_right"
            android:layout_toRightOf="@+id/column_to_left"
            android:fadingEdge="vertical"
            android:scrollbars="none" >
            <FrameLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:paddingLeft="5.0dp"
                android:paddingRight="5.0dp"
                android:paddingTop="3.0dp" >
                <ImageView android:id="@+id/column_slide_bar"
                    android:layout_width="49.0dp"
                    android:layout_height="29.0dp" />
                <LinearLayout android:id="@+id/column_title_layout"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal"
                    android:layout_gravity="center_vertical" />
            </FrameLayout>
        </HorizontalScrollView>
    </RelativeLayout>
</RelativeLayout>

其中的 column_slide_bar 就是那個會滑動的滑塊,而 column_title_layout 則用來容納所有子欄目。

在 string.xml 中加入欄目資料:

<string-array name="all_choice" translatable="false">
    <item>科技</item>
    <item>財經</item>
    <item>體育</item>
    <item>本地</item>
    <item>最新</item>
    <item>百家</item>
    <item>娛樂</item>
</string-array>

動態建立各個欄目標籤:

private void initTab() {
    String[] resource = this.getResources().getStringArray(R.array.all_choice);
    for (int j = 0; j < resource.length; j++) {
        array.add(resource[j]);
    }
    this.columnTitleLayout.removeAllViews();
    int j = this.array.size();
    if (j <= 5) {
        this.scrollToRight.setVisibility(View.INVISIBLE);
        this.scrollToLeft.setVisibility(View.INVISIBLE);
    }
    currTabIndex = 0;
    animImage.setBackgroundResource(R.drawable.slidebar);
    for (int i = 0; i < array.size(); i++) {
        TextView ColumnTextView = new TextView(this);
        ColumnTextView.setText(array.get(i));
        ColumnTextView.setTag(i);
        ColumnTextView.setPadding(18, 2, 15, 4);
        ColumnTextView.setOnClickListener(this);
        ColumnTextView.setTextAppearance(this, R.style.column_tx_style);
        LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        columnTitleLayout.addView(ColumnTextView, params);
    }
    // 最後補上「更多」欄目
    TextView MoreColumnTextView = new TextView(this);
    MoreColumnTextView.setTag(i);
    MoreColumnTextView.setText(getResources().getText(R.string.more_column));
    MoreColumnTextView.setPadding(18, 2, 15, 4);
    MoreColumnTextView.setTextAppearance(this, R.style.column_tx_style);
    LinearLayout.LayoutParams params =
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    columnTitleLayout.addView(MoreColumnTextView, params);
}

點擊子欄目時啟動動畫:

@Override
public void onClick(View v) {
    int k = (Integer) v.getTag();
    lastTabIndex = currTabIndex;
    currTabIndex = k;
    if (lastTabIndex != currTabIndex) {
        if (currTabIndex == array.size()) {
            return;
        }
        showAnimation();
    }
}

動畫採用 TranslateAnimation,並設定 setFillAfter(true) 讓滑塊停在目標位置:

private void showAnimation() {
    if (lastTabIndex == currTabIndex) {
        return;
    }
    ((TextView) columnTitleLayout.getChildAt(lastTabIndex)).setTextColor(R.drawable.white);
    int widgetItemWidth = ((TextView) columnTitleLayout.getChildAt(lastTabIndex)).getWidth();
    int fromX = lastTabIndex * widgetItemWidth;
    int toX = currTabIndex * widgetItemWidth;
    TranslateAnimation animation = new TranslateAnimation(fromX, toX, 0, 0);
    animation.setDuration(500);
    animation.setFillAfter(true);
    animation.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            ((TextView) columnTitleLayout.getChildAt(lastTabIndex))
                .setTextColor(MainActivity.this.getResources().getColor(R.drawable.gray2));
        }
        @Override
        public void onAnimationRepeat(Animation animation) {}
        @Override
        public void onAnimationEnd(Animation animation) {
            ((TextView) columnTitleLayout.getChildAt(currTabIndex))
                .setTextColor(MainActivity.this.getResources().getColor(R.drawable.white));
            lastTabIndex = currTabIndex;
        }
    });
    animImage.startAnimation(animation);
}

核心思路就是:以「欄目寬度 × 索引」算出滑塊的起點與終點 X 座標,再用 TranslateAnimation 平滑移動,同時切換文字顏色,就能呈現如新聞客戶端般流暢的欄目切換效果。

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