<pre name="code" class="java"><pre name="code" class="java">
//首先从anim文件夹下加载动画xml
public CustomTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
slideLeftIn = AnimationUtils.loadAnimation(context,R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(context,R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(context,R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(context,R.anim.slide_right_out);
}
//继承自TabHost并复写<span style="font-family: Arial, Helvetica, sans-serif;">setCurrentTab方法即可实现自定义切换动画效果</span>
@Override
public void setCurrentTab(int index) {
//index为要切换到的tab页索引,currentTabIndex为现在要当前tab页的索引
int currentTabIndex = getCurrentTab();
//设置当前tab页退出时的动画
if (null != getCurrentView()){//第一次进入MainActivity时,getCurrentView()取得的值为空
if (currentTabIndex == (tabCount - 1) && index == 0) {//处理边界滑动
getCurrentView().startAnimation(slideRightOut);
} else if (currentTabIndex == 0 && index == (tabCount - 1)) {//处理边界滑动
getCurrentView().startAnimation(slideLeftOut);
} else if (index > currentTabIndex) {//非边界情况下从右往左滑动
getCurrentView().startAnimation(slideLeftOut);
} else if (index < currentTabIndex) {//非边界情况下从左往右滑动
getCurrentView().startAnimation(slideRightOut);
}
}
super.setCurrentTab(index);//不知道为何要放在这里,放在其他地方效果就出不来了
//设置即将显示的tab页的动画
View currentView=getCurrentView();
if (currentTabIndex == (tabCount - 1) && index == 0){//处理边界滑动
if(currentView!=null)
currentView.startAnimation(slideLeftIn);
} else if (currentTabIndex == 0 && index == (tabCount - 1)) {//处理边界滑动
if(currentView!=null)
currentView.startAnimation(slideRightIn);
} else if (index > currentTabIndex) {//非边界情况下从右往左滑动
if(currentView!=null)
currentView.startAnimation(slideRightIn);
} else if (index < currentTabIndex) {//非边界情况下从左往右滑动
if(currentView!=null)
currentView.startAnimation(slideLeftIn);
}
}
参考自:http://helloandroid.iteye.com/blog/1181827,里面还介绍了如何实现手势滑动。