几种tabhost的总结(3)
activity动画切换效果
一;布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.ct.tabhost.AnimationTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/ll_messagehome_liner" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dp" android:layout_weight="1.0" android:gravity="center" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" android:visibility="gone" /> <RadioGroup android:id="@+id/rg_message_rgroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:gravity="center_vertical" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_message_rbutton0" style="@style/messagehome_rdbtn" android:drawableTop="@drawable/mes_image_bg" android:tag="sent" android:text="发送" /> <RadioButton android:id="@+id/rb_message_rbutton1" style="@style/messagehome_rdbtn" android:checked="true" android:drawableTop="@drawable/get_image_bg" android:tag="receive" android:text="收信" /> <RadioButton android:id="@+id/rb_message_rbutton2" style="@style/messagehome_rdbtn" android:drawableTop="@drawable/dele_image_bg" android:tag="rubbish" android:text="垃圾箱" /> <RadioButton android:id="@+id/rb_message_rbutton3" style="@style/messagehome_rdbtn" android:drawableTop="@drawable/write_image_bg" android:tag="drafts" android:text="草稿箱" /> </RadioGroup> </LinearLayout> </com.ct.tabhost.AnimationTabHost> </LinearLayout>
二:MainActivity
package com.ct.tabhost; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TabHost; import android.widget.TabHost.TabSpec; public class MainActivity extends TabActivity { /** Called when the activity is first created. */ public static RadioGroup group; AnimationTabHost tabHost; public static final String TAB_SENT = "sent"; public static final String TAB_RECEIVE = "receive"; public static final String TAB_RUBBISH = "rubbish"; public static final String TAB_DRAFTS = "drafts"; TabSpec tabSpec; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); tabListener(); } private void initView(){ // 单选按钮组 group = (RadioGroup) findViewById(R.id.rg_message_rgroup); addTabHost(); } /** * 增加Tabhost内容的. */ private void addTabHost() { tabHost = (AnimationTabHost) getTabHost(); tabHost.addTab(tabHost.newTabSpec(TAB_SENT).setIndicator(TAB_SENT) .setContent(new Intent(this, SendMessageActivity.class))); tabHost.addTab(tabHost.newTabSpec(TAB_RECEIVE) .setIndicator(TAB_RECEIVE).setContent( new Intent(this, ReceiveMessageAcitvity.class))); tabHost.addTab(tabHost.newTabSpec(TAB_RUBBISH) .setIndicator(TAB_RUBBISH).setContent( new Intent(this, RubbishMessageActivity.class))); tabHost.addTab(tabHost.newTabSpec(TAB_DRAFTS).setIndicator(TAB_DRAFTS) .setContent(new Intent(this, DraftsBoxActivity.class))); tabHost.setCurrentTab(1); tabHost.setOpenAnimation(true); } private void tabListener(){ // 切换监听 tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub System.out.println(tabId + "--<tabId"); } }); group.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rb_message_rbutton0: tabHost.setCurrentTab(0); // tabHost.setCurrentTabByTag(TAB_SENT); break; case R.id.rb_message_rbutton1: tabHost.setCurrentTab(1); // tabHost.setCurrentTabByTag(TAB_RECEIVE); break; case R.id.rb_message_rbutton2: tabHost.setCurrentTab(2); // tabHost.setCurrentTabByTag(TAB_RUBBISH); break; case R.id.rb_message_rbutton3: tabHost.setCurrentTab(3); // tabHost.setCurrentTabByTag(TAB_DRAFTS); break; default: break; } } }); } }
AnimationTabHost
package com.ct.tabhost; import android.content.Context; import android.util.AttributeSet; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.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); // TODO Auto-generated constructor stub 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); } @Override public void setCurrentTab(int index) { // TODO Auto-generated method stub 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); } } } }
几种动画
1、slide_left_in
<?xml version="1.0" encoding="utf-8"?> <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>
2、slide_left_out
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="800" android:fromXDelta="0" android:toXDelta="-100%p" /> <alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="1.0" /> </set>
3、slide_right_in
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true" > <translate android:duration="800" android:fromXDelta="100%p" android:toXDelta="0" /> <alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="1.0" /> </set>
4、slide_right_out
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="800" android:fromXDelta="0" android:toXDelta="100%p" /> <alpha android:duration="800" android:fromAlpha="1.0" android:toAlpha="1.0" /> </set>
(在F:\java\MyTabHost3)