2

我有一个自定义的滑出菜单实现(不使用任何库),在其中我将布局向右滑动并在左侧显示菜单(就像 facebook、google+ 应用程序一样)。显示菜单后,我通过提供一些 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;
}

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

4

1 回答 1

1

在你的

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

               alphaAnimation.setFillAfter(true);

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

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

于 2013-04-08T13:16:55.393 回答