package com.ctmytesttabanimation; import android.content.Context; import android.util.AttributeSet; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.TabHost; /** 继承 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; } /** * 返回当前标签页的总数 */ 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 (index > mCurrentTabID) { getCurrentView().startAnimation(slideLeftOut); } // 切换到左边的界面,从右边离开 else if (index < mCurrentTabID) { getCurrentView().startAnimation(slideRightOut); } } } // 设置当前页 super.setCurrentTab(index); if (isOpenAnimation) { // 当前页进来是动画 // 切换到右边的界面,从右边进来 if (index > mCurrentTabID) { getCurrentView().startAnimation(slideRightIn); } // 切换到左边的界面,从左边进来 else if (index < mCurrentTabID) { getCurrentView().startAnimation(slideLeftIn); } } } }
package com.ctmytesttabanimation; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TabHost.OnTabChangeListener; public class MainActivity extends TabActivity { private AnimationTabHost tabHost; RadioGroup group; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_tab); initView(); initTab(); tabListener(); } private void initView(){ group = (RadioGroup) findViewById(R.id.mypublic_rodio); } private void initTab(){ tabHost = (AnimationTabHost)getTabHost(); tabHost.addTab(tabHost.newTabSpec("notice").setIndicator("notice") .setContent(new Intent(this,AnnouncementActivity.class))); tabHost.addTab(tabHost.newTabSpec("document").setIndicator("document") .setContent(new Intent(this,RulesActivity.class))); tabHost.setOpenAnimation(true); } private void tabListener(){ tabHost.setOnTabChangedListener(new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub } }); group.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (checkedId) { case R.id.radio_button0: tabHost.setCurrentTabByTag("notice"); break; case R.id.radio_button1: tabHost.setCurrentTabByTag("document"); break; default: break; } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<?xml version="1.0" encoding="utf-8"?> <!-- slide_left_in --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="800" android:fromXDelta="-100%p" android:toXDelta="0" /> <alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="1.0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- slide_left_out --> <translate android:duration="800" android:fromXDelta="0" android:toXDelta="-100%p" /> <alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="1.0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true" > <!-- slide_right_in --> <translate android:duration="800" android:fromXDelta="100%p" android:toXDelta="0" /> <alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="1.0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- slide_right_out --> <translate android:duration="800" android:fromXDelta="0" android:toXDelta="100%p" /> <alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="1.0" /> </set>