Android Animation: Hide/Show Menu

2019-02-11 07:15发布

问题:

I am trying to add an animation to my app that will hide or show a menu on single tap. Basically something similar to Pulse news readers article view. I am able to animate the menu container. However,the menu does not slide down at the same time as the main container is creating space for the menu holder. I would like to know how to fix this issue.

Here is my animation code:

if(homeTabBar.getVisibility() == View.GONE){
    homeTabBar.setVisibility(View.VISIBLE);
    final Animation tabBlockHolderAnimation = AnimationUtils.loadAnimation(ArticleActivity.this, R.anim.tab_down);

    tabBlockHolderAnimation.setFillAfter(true);
    homeTabBar.startAnimation(tabBlockHolderAnimation);


}else{

    final Animation tabBlockHolderAnimation = AnimationUtils.loadAnimation(ArticleActivity.this, R.anim.tab_up);
    tabBlockHolderAnimation.setAnimationListener(new AnimationListener(){



    @Override
    public void onAnimationEnd(Animation animation) {

    // TODO Auto-generated method stub

    homeTabBar.setVisibility(View.GONE);
   }
});

tabBlockHolderAnimation.setFillAfter(true);

homeTabBar.startAnimation(tabBlockHolderAnimation);

回答1:

public void toggle() {
    TranslateAnimation anim = null;

    isOpen = !isOpen;

    if (isOpen) {
        layoutRoot.setVisibility(View.VISIBLE);
        anim = new TranslateAnimation(0.0f, 0.0f, layoutRoot.getHeight(), 0.0f);
    } else {
        anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, layoutRoot.getHeight());
        anim.setAnimationListener(collapseListener);
    }

    anim.setDuration(300);
    anim.setInterpolator(new AccelerateInterpolator(1.0f));
    layoutRoot.startAnimation(anim);
}

Animation.AnimationListener collapseListener = new Animation.AnimationListener() {
    public void onAnimationEnd(Animation animation) {
        layoutRoot.setVisibility(View.GONE);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }
};