Animation while switching Activity in Android?

此生再无相见时 提交于 2020-01-31 07:04:13

问题


I want an animation while switching from one activity to another in Android. The animation I'm aiming for is a bottom to top like animation.

How can I do that?


回答1:


Yes it is possible. check out this question. You have to define animations in anim folder than you can overide current animation using

overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );



回答2:


You can set your animation when you go to another activity using this

overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

Also you can get same animation if you come back from last activity to previous activity by overriding method

@Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

    }



回答3:


A better way to do this is to create a style like below

<style name="SlideAnimation.Activity" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/slide_from_top</item>
    <item name="android:activityOpenExitAnimation">@anim/slide_to_bottom</item>
    <item name="android:activityCloseEnterAnimation">@anim/slide_from_bottom</item>
    <item name="android:activityCloseExitAnimation">@anim/slide_to_top</item>
</style>

If you want to implement this for whole application then use it in app theme like be

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorWhite</item>
    <item name="colorPrimaryDark">@color/colorWhite</item>
    <item name="colorAccent">@color/colorAppBlue</item>
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="android:windowAnimationStyle">@style/SlideAnimation.Activity</item>
</style>

And declare the AppTheme in manifest in application tag like below-

<application
    android:name=".MyApp"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:theme="@style/AppTheme"
     >

And if you want to use for specific activity then apply the theme to that activity in manifest.




回答4:


You can override the public boolean onOptionsItemSelected(MenuItem item)function, and use finish() followed by overridePendingTransition().
For example, add the following code in your activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
    case android.R.id.home: // navigation up.
      finish();
      overridePendingTransition(R.anim.ENTER_ANIMATION, R.anim.EXIT_ANIMATION);
      return true;
    case ....: // implementation of other menu items.
  }
  return super.onOptionsItemSelected(item);
}

The other way is overwriting the public boolean onNavigateUp() function. But onNavigateUp() is only for API level 16 and above.



来源:https://stackoverflow.com/questions/7596976/animation-while-switching-activity-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!