Java 在 Android 应用上创建向下滑动动画的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18881886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:51:57  来源:igfitidea点击:

Best way to create slide down animation on Android app?

javaandroidbuttonslide

提问by Yi Son

I'm just getting into Android development and basically I have 5 rectangular buttons stacked on each other.

我刚刚进入 Android 开发,基本上我有 5 个相互堆叠的矩形按钮。

When I click one (let's say the top one), I want the other 4 to slide down, and another set of buttons or whatever to show between them.

当我单击一个(假设是最上面的)时,我希望其他 4 个向下滑动,并在它们之间显示另一组按钮或其他任何东西。

And I want the transitions to be sliding rather than just appearing.

我希望过渡是滑动的,而不是仅仅出现。

Any suggestions on how to implement that or what functions to use?

关于如何实现该功能或使用哪些功能的任何建议?

回答by Rishabh Srivastava

First you need to define the Animation in XML like this:

首先,您需要像这样在 XML 中定义动画:

Slide down from the top:

从顶部向下滑动:

<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromYDelta="-100%" android:toYDelta="0%" android:duration="1000"/>
</set>

Slide up out of the screen:

向上滑出屏幕:

<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromYDelta="0%" android:toYDelta="-100%" android:duration="1000"/>
</set>

You can load the Animation like this:

您可以像这样加载动画:

Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);

And then you can apply the Animation to your View like this:

然后你可以像这样将动画应用到你的视图中:

view.startAnimation(slide);