显示和隐藏用幻灯片向上/向下动画视图 [英] Show and hide a View with a slide up/down animation
问题描述
我有一个的LinearLayout
我要显示或隐藏与动画
的向上或向下推布局每当我改变它的知名度。
I have a LinearLayout
that I want to show or hide with an Animation
that pushes the layout upwards or downwards whenever I change its visibility.
我已经看到了几样在那里,但他们都不适合我的需要。
I've seen a few samples out there but none of them suit my needs.
我已经创建了两个XML文件的动画,但我不知道如何开始他们当我改变的LinearLayout
的知名度。
I have created two xml files for the animations but I do not know how to start them when I change the visibility of a LinearLayout
.
推荐答案
随着这是在的Android 3.0(蜂巢)推出了新的动画API是非常简单的创建这样的动画。
With the new animation API that was introduced in Android 3.0 (Honeycomb) it is very simple to create such animations.
滑动查看
下跌一段距离:
view.animate().translationY(distance);
您可以稍后滑动查看
返回到其像这样原来的位置:
You can later slide the View
back to its original position like this:
view.animate().translationY(0);
您也可以轻松地将多个动画。下面的动画将滑动查看
下降高度,并在同一时间淡化它:
You can also easily combine multiple animations. The following animation will slide a View
down by its height and fade it in at the same time:
// Prepare the View for the animation
view.setVisibility(View.VISIBLE);
view.setAlpha(0.0f);
// Start the animation
view.animate()
.translationY(view.getHeight())
.alpha(1.0f);
您就可以褪去查看
退了出来,并将其滑回原来的位置。我们还设置了 AnimatorListener
,所以我们可以设置的查看
返回 GONE能见度
一旦动画完成:
You can then fade the View
back out and slide it back to its original position. We also set an AnimatorListener
so we can set the visibility of the View
back to GONE
once the animation is finished:
view.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});
这篇关于显示和隐藏用幻灯片向上/向下动画视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!