在幻灯片菜单中滑动时淡入淡出布局

时间:2012-10-26 12:53:28

标签: android android-animation

我有一个自定义的幻灯片菜单实现(不使用任何库),我将我的布局向右滑动并在左侧显示菜单(如facebook,google + app确实)。显示菜单后,我通过给出一些alpha值来淡化正确的布局,如下面的代码所示:

    <FrameLayout
        android:id="@+id/fl_mask"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:alpha="0.7"
        android:background="@color/black">
    </FrameLayout>

但是当我的菜单显示时,这一切都会发生。我想要的是,当布局向右滑动时,我想让它变暗。从左边缘进一步布局,使布局更暗。此外,我使用以下代码进行布局动画,将我的布局向右滑动。

public Animation SlidingAnimation(int animateDuration, boolean slideLeftToright, final boolean executeOnAnimEndCode) {
    Animation animation = null;
    AnimationSet set = new AnimationSet(true);

    if (slideLeftToright) {
        animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f);
    } else {
        animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f);
    }

    animation.setDuration(animateDuration);

    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (executeOnAnimEndCode) {
                               ... // Some piece of code
            }
        }
    });
    set.addAnimation(animation);

    return animation;
}

当我向右滑动时,如何淡化/变暗我的布局?

1 个答案:

答案 0 :(得分:1)

在你的

public void onAnimationEnd(Animation animation) 
{
            if (executeOnAnimEndCode) 
            {
               AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);

               alphaAnimation.setFillAfter(true);

               someLayout.startAnimation(alphaAnimation);
            }
        }
    });

或者您也可以将动画声明为xml,然后使用它....