使活动从上到下动画化 [英] Make activity animate from top to bottom

查看:28
本文介绍了使活动从上到下动画化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Android 应用程序,我希望通过从屏幕底部到顶部的动画来显示 Activity.我可以用这里的代码来做到这一点:

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:

但是,我无法执行相反的动画,其中 Activity 会通过从屏幕顶部滑动到底部而消失.

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.

要查看的动画的重要部分是 fromYDeltatoYDelta 值.这些定义了开始时的 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() 方法中的挂起过渡:

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 shown
  • onDestroy() 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() 在 Activity 将被取消时调用
  • onResume() is called when the activity is shown to the user
  • onPause() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆