編輯:Android 開發實例
本文分享一個網易客戶端首頁導航條動畫的實作,重點在於點擊某個分類後,底下的指示條(ImageView)能平滑滑動到該項目下方,同時文字顏色跟著切換。

先來看佈局。最外層是 FrameLayout,內部包含一個 HorizontalScrollView、一組左右滾動用的 ImageButton,以及承載指示條的框架:
<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 觸發左右滾動;HorizontalScrollView 內部用的是 FrameLayout——因為 FrameLayout 是疊加式佈局,放在後面的元件會蓋在上層,所以 ImageView 刻意寫在 LinearLayout 之前,讓它落在文字下方,充當會移動的背景指示條。LinearLayout 裡放的是 TextView,由程式動態加入。
做法是為每個 TextView 掛上 onTouch 監聽,攔截 ACTION_DOWN(手指按下)時啟動一個 TranslateAnimation 平移動畫,動畫結束後再把 ImageView 實際的位置參數更新到新座標。位置調整是透過動態修改 TextView/ImageView 的版面參數達成的。
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 的 leftMargin 真正設為新座標——這個順序很重要,否則動畫過程中會看到指示條「跳一下」。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;
}
這段做得很直觀:先把上一個選中項的字色還原成黑色,再把目前點擊的項目設為白色,並記下參照以便下次還原。
HorizontalScrollView 的左右移動,靠的是在 ImageButton 的點擊事件中呼叫 smoothScrollTo(x, y),傳入新的座標:
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 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,因此要另外在 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>
以 onTouch 攔截點擊事件會讓 View 收不到原本的點擊回呼,若後續要掛 OnClickListener,記得在 ACTION_UP 時自行處理或回傳適當數值。此外,指示條位置以 leftMargin 記錄是可行但不夠健壯的做法——一旦項目寬度因字型或語言而改變,座標就會失準。較穩健的方式是在版面量測完成後,用 getLeft() 取得每個 TextView 的實際位置,再據此計算位移,並在 onConfigurationChanged 時重新計算一次。
Android MediaPlayer 深度說明:音訊播放控制與播放器範例
Android 提供多種播放音訊與影片的方式,而其中最廣為使用的莫過於 MediaPlayer 類別。它代表系統內建媒體播放服務的用戶端介面,能處理本機檔案、網路串流,音
Android 應用本地化:讓 App 自適應多語系環境
Android App 常在不同語言與地區的裝置上執行。若希望使用者看到符合當地語言的文字,就必須進行本地化。最常見的做法是把字串抽到 strings.xml,再依語言建
Android miniTwitter 登入畫面實作:漸層背景、圓角表單與相對佈局
登入畫面是許多 App 的第一印象。本文以 miniTwitter 登入頁為例,說明如何用漸層背景、圓角容器、相對佈局與表單控制項,做出一個清爽美觀的登入介面。 畫面結構
在 Eclipse 中導入 Android SDK Sample 專案的正確方式
Android SDK 內附的 Sample 範例是學習 Android 的重要資源。然而,若直接用一般 Import 方式,可能會把原始範例複製到工作區,或造成原目錄被