How To Implement Tabhost Sliding Effect?
I need to create some TabHost form paging. There is sliding animation effects in TabHost column when switching options page. I want to implement the page also has sliding animatio
Solution 1:
try this code:
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
/**
* A custom OnTabChangeListener that uses the TabHost its related to to fetch information about the current and previous
* tabs. It uses this information to perform some custom animations that slide the tabs in and out from left and right.
*
* @author Daniel Kvist
*
*/publicclassAnimatedTabHostListenerimplementsOnTabChangeListener
{
privatestaticfinalintANIMATION_TIME=240;
private TabHost tabHost;
private View previousView;
private View currentView;
privateint currentTab;
/**
* Constructor that takes the TabHost as a parameter and sets previousView to the currentView at instantiation
*
* @param tabHost
*/publicAnimatedTabHostListener(TabHost tabHost)
{
this.tabHost = tabHost;
this.previousView = tabHost.getCurrentView();
}
/**
* When tabs change we fetch the current view that we are animating to and animate it and the previous view in the
* appropriate directions.
*/@OverridepublicvoidonTabChanged(String tabId)
{
currentView = tabHost.getCurrentView();
if (tabHost.getCurrentTab() > currentTab)
{
previousView.setAnimation(outToLeftAnimation());
currentView.setAnimation(inFromRightAnimation());
}
else
{
previousView.setAnimation(outToRightAnimation());
currentView.setAnimation(inFromLeftAnimation());
}
previousView = currentView;
currentTab = tabHost.getCurrentTab();
}
/**
* Custom animation that animates in from right
*
* @return Animation the Animation object
*/private Animation inFromRightAnimation()
{
AnimationinFromRight=newTranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
return setProperties(inFromRight);
}
/**
* Custom animation that animates out to the right
*
* @return Animation the Animation object
*/private Animation outToRightAnimation()
{
AnimationoutToRight=newTranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
return setProperties(outToRight);
}
/**
* Custom animation that animates in from left
*
* @return Animation the Animation object
*/private Animation inFromLeftAnimation()
{
AnimationinFromLeft=newTranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
return setProperties(inFromLeft);
}
/**
* Custom animation that animates out to the left
*
* @return Animation the Animation object
*/private Animation outToLeftAnimation()
{
AnimationouttoLeft=newTranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
return setProperties(outtoLeft);
}
/**
* Helper method that sets some common properties
* @param animation the animation to give common properties
* @return the animation with common properties
*/private Animation setProperties(Animation animation)
{
animation.setDuration(ANIMATION_TIME);
animation.setInterpolator(newAccelerateInterpolator());
return animation;
}
}
Solution 2:
you can done this by using ViewPager and PageIndicator.
for compatibly with older Android versions, and more options.
use ViewPagerIndicator Lib https://github.com/JakeWharton/Android-ViewPagerIndicator/
Post a Comment for "How To Implement Tabhost Sliding Effect?"