Activity transition animation settings

In the company's project, there is a problem. Here is a summary
When an Activity is implicitly started, there will be a default start animation, which is a zoom effect. Every time other applications start music, there will be an obvious animation effect. If it is inconsistent with the design, it is necessary to remove the default animation, consult the relevant transition animation, and record here

How to set transition animation

  • Code settings
    Set by overridePendingTransition method. The code is as follows
    First create two xml files of animation
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:duration="2000"
        android:fromXDelta="0"
        android:toXDelta="-100%p"/>
 
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:duration="2000"
        android:fromXDelta="100%p"
        android:toXDelta="0"/>
 
</set>

The saved location is under the res anim folder
Reference in Code:

overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

What to pay attention to

  1. It only works on Android 2.0 and above
  2. It must be called immediately after startActivity() or finish() function.
    There is a problem in using this method. If other references start our application through implicit calls, we can't use this method. We need to use the following method, the theme method
  • How to set theme in xml file
<style name="AppTheme" parent="@style/Theme.AppCompat.NoActionBar">
        <item name="android:windowAnimationStyle">@style/Animation</item>
    </style>

    <style name="Animation">
        <item name="android:activityOpenEnterAnimation">@null</item>
        <item name="android:activityOpenExitAnimation">@null</item>
        <item name="android:activityCloseEnterAnimation">@null</item>
        <item name="android:activityCloseExitAnimation">@null</item>
        <item name="android:taskOpenEnterAnimation">@null</item>
        <item name="android:taskOpenExitAnimation">@null</item>
        <item name="android:taskCloseEnterAnimation">@null</item>
        <item name="android:taskCloseExitAnimation">@null</item>
        <item name="android:taskToFrontEnterAnimation">@null</item>
        <item name="android:taskToFrontExitAnimation">@null</item>
        <item name="android:taskToBackEnterAnimation">@null</item>
        <item name="android:taskToBackExitAnimation">@null</item>
    </style>

application can use this theme to clear the animation of Activity. Of course, you can specify animation in various ways in item
If you don't want to set the above for the entire app, you only want to set one activity

<activity android:name=".activity_shop.ShopHomeActivity" 
    android:theme="@style/Animation"/>

You can specify a theme separately

Tags: Android xml

Posted on Sat, 09 Nov 2019 13:31:35 -0500 by cbrian