編輯:關於 Android 程式設計
先看效果圖:

首先是版面配置檔。核心思路是在底部 Tab 上方覆蓋一個可滑動的 ImageView,用來呈現切換時的滑動動畫:
程式碼中先進行初始化,為每個分頁建立對應的 TabSpec 與指示器:
private void initTabHost() {
mTabHost = getTabHost();
mTabWidget = mTabHost.getTabWidget();
mTabHost.setCurrentTab(0);
// 以「首頁」為例,其餘分頁寫法相同
View homeWidgetView = mLayoutInflater.inflate(R.layout.tab_indicator, mTabWidget, false);
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(HOME_TAB);
TextView homeTitle = (TextView) homeWidgetView.findViewById(R.id.title);
homeTitle.setText(R.string.main_tab_home);
ImageView homeIcon = (ImageView) homeWidgetView.findViewById(R.id.icon);
homeIcon.setBackgroundResource(NORMAL_IMAGE[0]);
tabSpec = tabSpec.setIndicator(homeWidgetView).setContent(intent);
mTabHost.addTab(tabSpec);
// ...其餘 CHAT_TAB、MESSAGE_TAB、PROFILE_TAB、MORE_TAB 依此類推
mTabHost.setOnTabChangedListener(new TabHostListener(this));
animImage = (ImageView) findViewById(R.id.tab_widget_image);
animImage.setVisibility(View.INVISIBLE);
}
切換動畫透過滑動位移來呈現目前所在的分頁位置:
private void showAnimation() {
if (lastTabIndex == currTabIndex) return;
if (mTabWidget.getChildCount() < 2) return;
// 為支援橫屏,需重新設定子 view 的寬高
if (getResources().getConfiguration().orientation != lastImageChangeOrientation) {
lastImageChangeOrientation = getResources().getConfiguration().orientation;
widgetItemWidth = mTabWidget.getWidth() / mTabWidget.getChildCount();
View currView = mTabWidget.getChildAt(currTabIndex);
focusWidgetItemWidth = currView.getWidth();
focusWidgetItemHeight = currView.getHeight();
LayoutParams lp = animImage.getLayoutParams();
lp.width = focusWidgetItemWidth;
lp.height = focusWidgetItemHeight;
animImage.setLayoutParams(lp);
}
int fromX = lastTabIndex * widgetItemWidth;
int toX = currTabIndex * widgetItemWidth;
TranslateAnimation animation = new TranslateAnimation(fromX, toX, 0, 0);
animation.setDuration(600);
animation.setAnimationListener(new AnimationListener() {
@Override public void onAnimationStart(Animation animation) {
mTabWidget.getChildAt(lastTabIndex).setBackgroundResource(R.drawable.tab_bottom);
((ImageView) mTabWidget.getChildAt(lastTabIndex).findViewById(R.id.icon))
.setImageResource(NORMAL_IMAGE[lastTabIndex]);
((TextView) mTabWidget.getChildAt(lastTabIndex).findViewById(R.id.title))
.setTextColor(getResources().getColor(R.drawable.gray2));
animImage.setVisibility(View.VISIBLE);
}
@Override public void onAnimationRepeat(Animation animation) { }
@Override public void onAnimationEnd(Animation animation) {
animImage.setVisibility(View.INVISIBLE);
((ImageView) mTabWidget.getChildAt(currTabIndex).findViewById(R.id.icon))
.setImageResource(SELECTED_IMAGE[currTabIndex]);
((TextView) mTabWidget.getChildAt(currTabIndex).findViewById(R.id.title))
.setTextColor(getResources().getColor(R.color.white));
mTabWidget.getChildAt(currTabIndex).setBackgroundResource(R.drawable.tab_bottom_selected);
lastTabIndex = currTabIndex;
}
});
animImage.startAnimation(animation);
}
private class TabHostListener implements TabHost.OnTabChangeListener {
Context context;
public TabHostListener(Context context) { this.context = context; }
public void onTabChanged(String paramString) {
lastTabIndex = currTabIndex;
currTabIndex = mTabHost.getCurrentTab();
if (lastTabIndex != currTabIndex) {
showAnimation();
}
}
}
整體關鍵在於:透過覆蓋在 TabWidget 之上的 ImageView,配合 TranslateAnimation 做水平位移,就能模擬出類似微博客戶端那樣流暢的分頁切換動畫。
Android 自定義 View 進階:Matrix 全面詳解
上一篇談 Matrix 原理時偏理論,本文則聚焦具體用法,並補上幾個實務上真的會用到的技巧。這大概是目前中文世界對 Matrix 說明最完整的一篇。 方法總覽 先建立地
RecyclerView 實作複雜首頁布局的三種方式
電商、影音與內容平台的首頁,經常需要同時呈現橫幅、列表、網格與瀑布流。本文以 RecyclerView 為核心,整理三種常見實作方式,並比較其適用性。 方案一:Rec
Android 5.0 Toolbar 使用指南:取代 ActionBar 與獨立控件
Toolbar 的兩種定位 Toolbar 可以作為 ActionBar 使用,也可以視為獨立控件。實務上常見做法是讓 Toolbar 取代 ActionBar,以獲得更
Android 5.0 的 Material Design:從主題到新控件
Google 推出的 Material Design 設計規範,把扁平化的視覺語言與明亮配色結合在一起,配合層次分明的材質感,讓應用界面呈現出一種煥然一新的質感。 Mat