Android 中的 Activity 转换

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

Activity transition in Android

androidandroid-activitytransition

提问by hpique

How can I define the transition between two activities for Android 1.5 and later? I would like an activity to fade in.

如何为 Android 1.5 及更高版本定义两个 Activity 之间的转换?我想要一个活动淡入。

采纳答案by iandisme

You can do this with Activity.overridePendingTransition(). You can define simple transition animations in an XML resource file.

您可以使用Activity.overridePendingTransition(). 您可以在 XML 资源文件中定义简单的过渡动画。

回答by Ben Clayton

Here's the code to do a nice smooth fade between two Activities..

这是在两个活动之间做一个很好的平滑淡入淡出的代码..

Create a file called fadein.xmlin res/anim

创建一个名为文件fadein.xmlres/anim

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/accelerate_interpolator"
   android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />

Create a file called fadeout.xmlin res/anim

创建一个名为文件fadeout.xmlres/anim

<?xml version="1.0" encoding="utf-8"?>

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/accelerate_interpolator"
   android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" />

If you want to fade from Activity Ato Activity B, put the following in the onCreate()method for Activity B. Before setContentView()works for me.

如果你想从淡出活动A活动B,把下面的onCreate()对法活动B。之前setContentView()为我工作。

overridePendingTransition(R.anim.fadein, R.anim.fadeout);

If the fades are too slow for you, change android:durationin the xml files above to something smaller.

如果淡入淡出对您来说太慢,请将android:duration上面的 xml 文件更改为较小的文件。

回答by Felipe Conde

An even easy way to do it is:

一个更简单的方法是:

  1. Create an animation style into your styles.xml file
  1. 在styles.xml 文件中创建动画样式
<style name="WindowAnimationTransition">
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
    <item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>
<style name="WindowAnimationTransition">
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
    <item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style>
  1. Add this style to your app theme
  1. 将此样式添加到您的应用主题
<style name="AppBaseTheme" parent="Theme.Material.Light.DarkActionBar">
      <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
</style>
<style name="AppBaseTheme" parent="Theme.Material.Light.DarkActionBar">
      <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
</style>

That's it :)

就是这样 :)

回答by CaseyB

Yes. You can tell the OS what kind of transition you want to have for your activity.

是的。您可以告诉操作系统您希望为您的活动进行什么样的转换。

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getWindow().setWindowAnimations(ANIMATION);

    ...

}

Where ANIMATION is an integer referring to a built in animation in the OS.

其中 ANIMATION 是一个整数,指的是操作系统中的内置动画。

回答by Kevin C. Krinke

For a list of default animations see: http://developer.android.com/reference/android/R.anim.html

有关默认动画的列表,请参阅:http: //developer.android.com/reference/android/R.anim.html

There is in fact fade_inand fade_outfor API level 1 and up.

实际上fade_infade_out对于 API 级别 1 及更高级别。

回答by IceSteve

create res>anim>fadein.xml

创建 res>anim>fadein.xml

<?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />

create res>anim>fadeout.xml

创建 res>anim>fadeout.xml

<?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />

In res>values>styles.xml

在 res>values>styles.xml

<style name="Fade">
        <item name="android:windowEnterAnimation">@anim/fadein</item>
        <item name="android:windowExitAnimation">@anim/fadeout</item>
    </style>

In activities onCreate()

在 onCreate() 活动中

getWindow().getAttributes().windowAnimations = R.style.Fade;

回答by Shohan Ahmed Sijan

I overwrite my default activity animation. I test it in api 15 that it work smoothly. Here is the solution that I use:

我覆盖了我的默认活动动画。我在 api 15 中测试它运行顺利。这是我使用的解决方案:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item>

</style>

<style name="CustomActivityAnimation" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
    <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
    <item name="android:activityCloseEnterAnimation">@anim/slide_in_left</item>
    <item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>
</style>

Create anim folder under res folder and then create this four animation files:

在 res 文件夹下创建 anim 文件夹,然后创建这四个动画文件:

slide_in_right.xml

slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

slide_out_left.xml

slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

slide_in_left.xml

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

slide_out_right.xml

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
</set>

You can download my sample project.

您可以下载我的示例项目

That's all... :)

就这样... :)

回答by sachin pangare

Here's the code to do a nice smooth between two activity.

这是在两个活动之间进行良好平滑的代码。

  1. smooth effect from left to right

    Create a file called slide_in_right.xml and slide_out_right.xml in res/anim

    slide_in_right.xml

        <?xml version="1.0" encoding="utf-8"?>
        <set xmlns:android="http://schemas.android.com/apk/res/android"
            android:shareInterpolator="false" >
            <translate android:duration="5000" android:fromXDelta="100%" android:toXDelta="0%" />
            <alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
        </set>
    

    slide_out_right.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false" >
        <translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="-100%"/>
        <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
    </set>
    
  2. smooth effect from right to left

    Create a file called animation_enter.xml and animation_leave.xml in res/anim

    animation_enter.xml

       <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false">
        <translate android:fromXDelta="-100%" android:toXDelta="0%"
            android:fromYDelta="0%" android:toYDelta="0%"
            android:duration="700"/>
       </set>
    

    animation_leave.xml

      <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false">
        <translate
            android:fromXDelta="0%" android:toXDelta="100%"
            android:fromYDelta="0%" android:toYDelta="0%"
            android:duration="700" />
      </set>
    
  3. Navigate from one activity to second Activity

       Intent intent_next=new Intent(One_Activity.this,Second_Activity.class);
       overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
       startActivity(intent_next);
     finish();
    

    4.On back press event or Navigate from second activity to one Activity

     Intent home_intent = new Intent(Second_Activity.this, One_Activity.class);
     overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
     startActivity(home_intent);
     finish();
    
  1. 从左到右的平滑效果

    在 res/anim 中创建一个名为 slide_in_right.xml 和 slide_out_right.xml 的文件

    slide_in_right.xml

        <?xml version="1.0" encoding="utf-8"?>
        <set xmlns:android="http://schemas.android.com/apk/res/android"
            android:shareInterpolator="false" >
            <translate android:duration="5000" android:fromXDelta="100%" android:toXDelta="0%" />
            <alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
        </set>
    

    slide_out_right.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false" >
        <translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="-100%"/>
        <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
    </set>
    
  2. 从右到左的平滑效果

    在 res/anim 中创建一个名为 animation_enter.xml 和 animation_leave.xml 的文件

    动画输入.xml

       <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false">
        <translate android:fromXDelta="-100%" android:toXDelta="0%"
            android:fromYDelta="0%" android:toYDelta="0%"
            android:duration="700"/>
       </set>
    

    animation_leave.xml

      <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false">
        <translate
            android:fromXDelta="0%" android:toXDelta="100%"
            android:fromYDelta="0%" android:toYDelta="0%"
            android:duration="700" />
      </set>
    
  3. 从一个活动导航到第二个活动

       Intent intent_next=new Intent(One_Activity.this,Second_Activity.class);
       overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
       startActivity(intent_next);
     finish();
    

    4.返回按下事件或从第二个活动导航到一个活动

     Intent home_intent = new Intent(Second_Activity.this, One_Activity.class);
     overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);
     startActivity(home_intent);
     finish();
    

回答by Curtain

You cannot use overridePendingTransition in Android 1.5. overridePendingTransistion came to Android 2.0.

您不能在 Android 1.5 中使用 overridePendingTransition。overridePendingTransition 出现在 Android 2.0 中。

If you're gonna go through this without any error you have to compile for the target (1.5 or higher) using the ordinary animations (or you own) or you have to compile for the target (2.0 or higher) using overridePendingTransistion.

如果您要在没有任何错误的情况下完成此操作,则必须使用普通动画(或您自己的动画)为目标(1.5 或更高版本)编译,或者必须使用 overridePendingTransistion 为目标(2.0 或更高版本)编译。

Summary: You cannot use overridePendingTransistion in Android 1.5.

总结:您不能在 Android 1.5 中使用 overridePendingTransistion

You can though use the built-in animations in the OS.

您可以使用操作系统中的内置动画。

回答by Maher Ismaail

IN GALAXY Devices :

在 GALAXY 设备中:

You need to make sure that you havn't turned it off in the device using the Settings > Developer Options:

您需要确保您没有使用“设置”>“开发人员选项”在设备中将其关闭:

two muppets

两个布偶