自定义tabhost实现tab切换动画

来源:互联网 发布:致富360是什么软件 编辑:程序博客网 时间:2024/06/06 14:22
<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>
@Overridepublic 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,里面还介绍了如何实现手势滑动。

0 0
原创粉丝点击