Android 使活动从上到下动画化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23578059/
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-20 07:16:53  来源:igfitidea点击:

Make activity animate from top to bottom

androidandroid-animationandroid-transitions

提问by user2903200

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:

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

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.

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

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.

我使用了上面链接中的代码;活动通过向上滑动出现,但在消失时淡出,而不是滑到底部。

I even tried putting the code in onCreate():

我什至尝试将代码放入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();
}

回答by Richard Le Mesurier

You need to define your "slide up" animations from the linked question, and some new "slide down" animations that reverse the process.

您需要从链接的问题中定义“向上滑动”动画,以及一些反转过程的新“向下滑动”动画。

The important parts of the animations to look at are the fromYDeltaand toYDeltavalues. These define the Y-positions (of the top of your view) at the start & end of the animations.

要查看的动画的重要部分是fromYDeltatoYDelta值。这些定义了动画开始和结束时的 Y 位置(视图顶部)。

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" />


For the "slide up" animations, you should have overridden the pending transition in your onResume()method like this:

对于“向上滑动”动画,您应该onResume()像这样覆盖方法中的挂起过渡:

protected void onResume()
{
    super.onResume();
    overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
}

For the "slide down" animations, do something similar in your onPause()method:

对于“下滑”动画,请在您的onPause()方法中执行类似操作:

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()is not called every time the activity is shown
  • onDestroy()is not called every time the activity is taken away
  • onCreate()每次显示活动时都不会调用
  • onDestroy()每次活动被取消时都不会被调用

Rather use methods that are called every time there is a screen transition:

而是使用每次出现屏幕转换时调用的方法:

  • onResume()is called when the activity is shown to the user
  • onPause()is called when the activity is going to be taken away
  • onResume()当活动显示给用户时调用
  • onPause()当活动将被取消时调用

For more info on these methods specifically, check the Android developer site:

有关这些方法的更多信息,请查看 Android 开发者网站:



When your screen is displayed, it will slide in from the bottom.

当您的屏幕显示时,它将从底部滑入。

When a new screen is displayed, your screen will slide back down.

当显示新屏幕时,您的屏幕将向下滑动。

回答by nyx

Two ways of doing this:

这样做的两种方法:

1. Using styles

1. 使用样式

Assuming you wish to implement this for all activities, in your base theme define the following entry:

假设您希望为所有活动实现这一点,请在您的基本主题中定义以下条目:

<item name="android:windowAnimationStyle">@style/ActivityAnimations</item>

<item name="android:windowAnimationStyle">@style/ActivityAnimations</item>

Then define the following style:

然后定义如下样式:

<style name="ActivityAnimations" parent="android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/appear_from_bottom</item>
    <item name="android:activityOpenExitAnimation">@anim/hold</item>
    <item name="android:activityCloseEnterAnimation">@anim/hold</item>
    <item name="android:activityCloseExitAnimation">@anim/disappear_to_bottom</item>
</style>

Where @anim/holdcan be something like this:

哪里@anim/hold可以是这样的:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate
        android:duration="1000"
        android:zAdjustment="bottom" />
</set>

2. Using overridePendingTransition()

2. 使用 overridePendingTransition()

Override finish():

覆盖完成():

  @Override
  public void finish() {
    super.finish();
    overridePendingTransition(R.anim.hold, R.anim.disappear_to_bottom);
  }

Override onBackPressed():

覆盖 onBackPressed():

  @Override
  public void onBackPressed() {
    finish();
  }

Override onOptionsItemSelected():

覆盖 onOptionsItemSelected():

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
  }

回答by Usman Afzal

In your main xml file. Add id of the root tag and pass this to function. like

在您的主 xml 文件中。添加根标签的 id 并将其传递给函数。喜欢

/** The Constant ANIM_NO. */
public static final int ANIM_NO = 0;

public static void topToDown(Context context, View target, int type,
        int duration) {

    if (type == UtilityAnimations.ANIM_NO) {
        UtilityAnimations.topToDown(context, target, duration);
    } else {
        final Animation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, -1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f);
        animation.setDuration(duration != 0 ? duration
                : UtilityAnimations.DURATION_MEDIAM);
        animation.setInterpolator(UtilityAnimations.getInterpolator(
                context, type));
        target.startAnimation(animation);
    }
}

回答by Arash GM

you can vice-versa your transition by overriding the Transition in your onPause() :

您可以通过覆盖 onPause() 中的 Transition 来反转您的过渡:

@Override
protected void onPause()
{
    super.onPause();
    overridePendingTransition(R.anim.appear_from_bottom, R.anim.disappear_to_bottom);

}