Android动画:隐藏/显示菜单

时间:2011-08-15 20:12:01

标签: android layout animation menu show-hide

我正在尝试向我的应用添加动画,该动画将隐藏或在单击时显示菜单。基本上类似于Pulse新闻阅读器的文章观点。我能够为菜单容器设置动画。但是,菜单不会在主容器为菜单持有者创建空间的同时向下滑动。我想知道如何解决这个问题。

这是我的动画代码:

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 个答案:

答案 0 :(得分:13)

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