关键点是重写setCurrentTab(int index) 方法。
代码片段:
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TabHost;
import com.wjj.ath.R;
/** 继承 TabHost 组件,带有切入切出的滑动动画效果。 */
public class AnimationTabHost extends TabHost {
private Animation slideLeftIn;// 从屏幕左边进来
private Animation slideLeftOut;// 从屏幕左边出去
private Animation slideRightIn;// 从屏幕右边进来
private Animation slideRightOut;// 从屏幕右边出去
/** 记录是否打开动画效果 */
private boolean isOpenAnimation;
/** 记录当前标签页的总数 */
private int mTabCount;
public AnimationTabHost(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);
isOpenAnimation = false;// 动画默认关闭
}
/**
* 设置是否打开动画效果
*
* @param isOpenAnimation
* true:打开
*/
public void setOpenAnimation(boolean isOpenAnimation) {
this.isOpenAnimation = isOpenAnimation;
}
/**
*
* @return 返回当前标签页的总数
*/
public int getTabCount() {
return mTabCount;
}
@Override
public void addTab(TabSpec tabSpec) {
mTabCount++;
super.addTab(tabSpec);
}
// 重写setCurrentTab(int index) 方法,这里加入动画!关键点就在这。
@Override
public void setCurrentTab(int index) {
// 切换前所在页的页面
int mCurrentTabID = getCurrentTab();
if (null != getCurrentView()) {
// 第一次设置 Tab 时,该值为 null。
if (isOpenAnimation) {
// 离开的页面
// 循环时,末页到第一页(边界处理)
if (mCurrentTabID == (mTabCount - 1) && index == 0) {
getCurrentView().startAnimation(slideLeftOut);
}
// 循环时,首页到末页
else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {
getCurrentView().startAnimation(slideRightOut);
}
// 切换到右边的界面,从左边离开
else if (index > mCurrentTabID) {
getCurrentView().startAnimation(slideLeftOut);
}
// 切换到左边的界面,从右边离开
else if (index < mCurrentTabID) {
getCurrentView().startAnimation(slideRightOut);
}
}
}
// 设置当前页
super.setCurrentTab(index);
if (isOpenAnimation) {
// 当前页进来是动画
// 循环时,末页到第一页
if (mCurrentTabID == (mTabCount - 1) && index == 0) {
getCurrentView().startAnimation(slideRightIn);
}
// 循环时,首页到末页(边界处理)
else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {
getCurrentView().startAnimation(slideLeftIn);
}
// 切换到右边的界面,从右边进来
else if (index > mCurrentTabID) {
getCurrentView().startAnimation(slideRightIn);
}
// 切换到左边的界面,从左边进来
else if (index < mCurrentTabID) {
getCurrentView().startAnimation(slideLeftIn);
}
}
}
}
package com.wjj.ath.activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import com.wjj.ath.R;
import com.wjj.ath.widget.AnimationTabHost;
public class TabHostActivity extends TabActivity implements OnTabChangeListener {
private GestureDetector gestureDetector;
private AnimationTabHost mTabHost;
private TabWidget mTabWidget;
/** 记录当前分页ID */
private int currentTabID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (AnimationTabHost) findViewById(android.R.id.tabhost);
mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
mTabHost.setOnTabChangedListener(this);
init();
onTabChanged("0");// 人为调用回调方法,初始化选项卡tabs的颜色
gestureDetector = new GestureDetector(new TabHostTouch());
}
private void init() {
setIndicator(R.drawable.icon_1_c, 0, new Intent(this,
TabHostTestOne.class));
setIndicator(R.drawable.icon_2_c, 1, new Intent(this,
TabHostTestTwo.class));
setIndicator(R.drawable.icon_3_c, 2, new Intent(this,
TabHostTestThree.class));
setIndicator(R.drawable.icon_4_c, 3, new Intent(this,
TabHostTestFour.class));
mTabHost.setOpenAnimation(true);
}
private void setIndicator(int icon, int tabId, Intent intent) {
String str = String.valueOf(tabId);
TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(str)
.setIndicator(str, getResources().getDrawable(icon))
.setContent(intent);
mTabHost.addTab(localTabSpec);
}
@Override
public void onTabChanged(String tabId) {
// tabId 为newTabSpec(String tag) 中传入的字符串tag,这里tag是0,1,2,3 可以转换为整形便于判断
int tabID = Integer.valueOf(tabId);
for (int i = 0; i < mTabWidget.getChildCount(); i++) {
if (i == tabID) {
mTabWidget.getChildAt(i).setBackgroundResource(
R.drawable.indicator_selected);
} else {
mTabWidget.getChildAt(i).setBackgroundResource(
R.drawable.indicator_unselected);
}
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}
private class TabHostTouch extends SimpleOnGestureListener {
/** 滑动翻页所需距离 */
private static final int ON_TOUCH_DISTANCE = 80;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// 右滑动,切换到左边一个tab
if (e2.getX() - e1.getX() >= ON_TOUCH_DISTANCE) {
currentTabID = mTabHost.getCurrentTab() - 1;
if (currentTabID < 0) {// 循环
currentTabID = mTabHost.getTabCount() - 1;
}
}
// 左滑动,切换到右边一个tab
else if (e1.getX() - e2.getX() >= ON_TOUCH_DISTANCE) {
currentTabID = mTabHost.getCurrentTab() + 1;
if (currentTabID >= mTabHost.getTabCount()) {// 循环
currentTabID = 0;
}
}
mTabHost.setCurrentTab(currentTabID);
return false;
}
}
}
