使活动从上到下进行动画处理 [英] Make activity animate from top to bottom
问题描述
我正在编写一个Android应用程序,我希望通过从屏幕底部到顶部的动画来显示活动.我可以使用以下代码执行此操作:
I am writing an Android app where I want the activity to appear by animating in from the bottom of the screen to the top. I am able to do this with code from here:
但是,反之亦然,我无法制作动画,因为从屏幕顶部到底部滑动,活动将消失.
However, I am not able to do the vice-versa animation wherein the Activity would disappear by sliding from the top to the bottom of the screen.
我使用了上面链接中的代码;活动会通过向上滑动而出现,但消失后会逐渐消失,而不是滑到底部.
I used the code in the above link; the activity appears by sliding up, but when disappearing, it fades out, instead of sliding to the bottom.
我什至尝试将代码放入onCreate()
:
I even tried putting the code in onCreate()
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
overridePendingTransition(R.anim.appear_from_bottom, R.anim.disappear_to_bottom);
setContentView(R.layout.activity_all_metadata_display);
initializePage();
}
推荐答案
您需要根据链接的问题定义向上滑动"动画,并定义一些与之相反的新向下滑动"动画.
You need to define your "slide up" animations from the linked question, and some new "slide down" animations that reverse the process.
要看的动画的重要部分是fromYDelta
和toYDelta
值.它们定义了开始位置和视图顶部的Y位置(位于视图顶部).动画的结尾.
The important parts of the animations to look at are the fromYDelta
and toYDelta
values. These define the Y-positions (of the top of your view) at the start & end of the animations.
slide_in_up.xml
slide_in_up.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="100%p"
android:toYDelta="0%p" />
slide_out_up.xml
slide_out_up.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="0%p"
android:toYDelta="-100%p" />
slide_in_down.xml
slide_in_down.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="-100%p"
android:toYDelta="0%p" />
slide_out_down.xml
slide_out_down.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="0%p"
android:toYDelta="100%p" />
对于向上滑动"动画,您应该像这样在onResume()
方法中覆盖待处理的过渡:
For the "slide up" animations, you should have overridden the pending transition in your onResume()
method like this:
protected void onResume()
{
super.onResume();
overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
}
对于向下滑动"动画,请在您的onPause()
方法中执行类似的操作:
For the "slide down" animations, do something similar in your onPause()
method:
protected void onPause()
{
super.onPause();
overridePendingTransition(R.anim.slide_in_down, R.anim.slide_out_down);
}
一些教程建议使用错误的生命周期方法:
Some tutorials suggest using the wrong life-cycle methods:
-
每次显示活动时都不会调用
-
onCreate()
每次取走活动时都不会调用 -
onDestroy()
onCreate()
is not called every time the activity is shownonDestroy()
is not called every time the activity is taken away
使用每次屏幕转换时都会调用的方法:
Rather use methods that are called every time there is a screen transition:
-
当向用户显示活动时,会调用
-
onResume()
要取消活动时调用 -
onPause()
onResume()
is called when the activity is shown to the useronPause()
is called when the activity is going to be taken away
有关这些方法的详细信息,请访问Android开发者网站:
For more info on these methods specifically, check the Android developer site:
显示屏幕时,它将从底部滑入.
When your screen is displayed, it will slide in from the bottom.
显示新屏幕时,您的屏幕将向下滑动.
When a new screen is displayed, your screen will slide back down.
这篇关于使活动从上到下进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!