I'm trying to add an animation to my TabActivty. For example, when the user select the 2nd tab, I want the new activity comes from the right. When the user select the first tab, I want the activity comes from the left.
I've found how to add one animation, but I want to add one again. Here is the code I'm using:
public Animation inFromRightAnimation()
{
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(240);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
And
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId)
{
View currentView = getTabHost().getCurrentView();
currentView.setAnimation( inFromRightAnimation() );
}
});
How can I do that?
Thanks.
Regards.
V.
This works correctly:
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId)
{
View currentView = getTabHost().getCurrentView();
if (getTabHost().getCurrentTab() > currentTab)
{
currentView.setAnimation( inFromRightAnimation() );
}
else
{
currentView.setAnimation( outToRightAnimation() );
}
currentTab = getTabHost().getCurrentTab();
}
});
And the animations:
public Animation inFromRightAnimation()
{
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(240);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
public Animation outToRightAnimation()
{
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(240);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
I wrote a custom OnTabChangeListener based on this code that I wanted to share. Hopefully someone can use it :). Credit goes to Vomenki for original code.
package net.danielkvist.receipttracker.listener;
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
*
*/
public class AnimatedTabHostListener implements OnTabChangeListener
{
private static final int ANIMATION_TIME = 240;
private TabHost tabHost;
private View previousView;
private View currentView;
private int currentTab;
/**
* Constructor that takes the TabHost as a parameter and sets previousView to the currentView at instantiation
*
* @param tabHost
*/
public AnimatedTabHostListener(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.
*/
@Override
public void onTabChanged(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()
{
Animation inFromRight = new TranslateAnimation(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()
{
Animation outToRight = new TranslateAnimation(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()
{
Animation inFromLeft = new TranslateAnimation(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()
{
Animation outtoLeft = new TranslateAnimation(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(new AccelerateInterpolator());
return animation;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With