Android Animation: Hide/Show Menu

一笑奈何 提交于 2020-01-21 03:57:07

问题


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) {
    }
};


来源:https://stackoverflow.com/questions/7070225/android-animation-hide-show-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!