Can I change the Android startActivity() transition animation?

后端 未结 10 1626
臣服心动
臣服心动 2020-11-28 19:53

I am starting an activity and would rather have a alpha fade-in for startActivity(), and a fade-out for the finish(). How can I go about this in th

相关标签:
10条回答
  • 2020-11-28 20:09

    Starting from API level 5 you can call overridePendingTransition immediately to specify an explicit transition animation:

    startActivity();
    overridePendingTransition(R.anim.hold, R.anim.fade_in);
    

    or

    finish();
    overridePendingTransition(R.anim.hold, R.anim.fade_out);
    
    0 讨论(0)
  • 2020-11-28 20:09

    If you always want to the same transition animation for the activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    
    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing()) {
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 20:09

    I wanted to use the styles.xml solution, but it did not work for me with activities. Turns out that instead of using android:windowEnterAnimation and android:windowExitAnimation, I need to use the activity animations like this:

    <style name="ActivityAnimation.Vertical" parent="">
        <item name="android:activityOpenEnterAnimation">@anim/enter_from_bottom</item>
        <item name="android:activityOpenExitAnimation">@anim/exit_to_bottom</item>
        <item name="android:activityCloseEnterAnimation">@anim/enter_from_bottom</item>
        <item name="android:activityCloseExitAnimation">@anim/exit_to_bottom</item>
        <item name="android:windowEnterAnimation">@anim/enter_from_bottom</item>
        <item name="android:windowExitAnimation">@anim/exit_to_bottom</item>
    </style>
    

    Also, for some reason this only worked from Android 8 and above. I added the following code to my BaseActivity, to fix it for the API levels below:

    override fun finish() {
        super.finish()
        setAnimationsFix()
    }
    
    /**
     * The activityCloseExitAnimation and activityCloseEnterAnimation properties do not work correctly when applied from the theme.
     * So in this fix, we retrieve them from the theme, and apply them.
     * @suppress Incorrect warning: https://stackoverflow.com/a/36263900/1395437
     */
    @SuppressLint("ResourceType")
    private fun setAnimationsFix() {
        // Retrieve the animations set in the theme applied to this activity in the manifest..
        var activityStyle = theme.obtainStyledAttributes(intArrayOf(attr.windowAnimationStyle))
        val windowAnimationStyleResId = activityStyle.getResourceId(0, 0)
        activityStyle.recycle()
        // Now retrieve the resource ids of the actual animations used in the animation style pointed to by
        // the window animation resource id.
        activityStyle = theme.obtainStyledAttributes(windowAnimationStyleResId, intArrayOf(activityCloseEnterAnimation, activityCloseExitAnimation))
        val activityCloseEnterAnimation = activityStyle.getResourceId(0, 0)
        val activityCloseExitAnimation = activityStyle.getResourceId(1, 0)
        activityStyle.recycle()
        overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
    }
    
    0 讨论(0)
  • 2020-11-28 20:10
     // CREATE anim 
    
     // CREATE animation,animation2  xml // animation like fade out 
    
      Intent myIntent1 = new Intent(getApplicationContext(), Attend.class);
      Bundle bndlanimation1 =  ActivityOptions.makeCustomAnimation(getApplicationContext(), 
      R.anim.animation, R.anim.animation2).toBundle();
      tartActivity(myIntent1, bndlanimation1);
    
    0 讨论(0)
  • 2020-11-28 20:12

    Use overridePendingTransition

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

    fadein.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <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" />
    </set>
    

    fadeout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <alpha xmlns:android="http://schemas.android.com/apk/res/android"
            android:interpolator="@android:anim/anticipate_interpolator"
            android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
    </set>
    
    0 讨论(0)
  • 2020-11-28 20:15

    See themes on android: http://developer.android.com/guide/topics/ui/themes.html.

    Under themes.xml there should be android:windowAnimationStyle where you can see the declaration of the style in styles.xml.

    Example implementation:

    <style name="AppTheme" parent="...">
    
        ...
    
        <item name="android:windowAnimationStyle">@style/WindowAnimationStyle</item>
    
    </style>
    
    <style name="WindowAnimationStyle">
        <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
        <item name="android:windowExitAnimation">@android:anim/fade_out</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题