Android uses Theme to customize the switching animation between activities

Keywords: Android xml encoding

Source of reference: http://blog.csdn.net/lmj623565791/article/details/22990643

In most Android systems, the default animation switching effect between activities is slide in on the right and slide out on the left; sometimes our requirement may be to require all activities to be switched in and out, at this time, we may need to change the default switching style.

Let's start with:

First, create anim folder in res folder, and then create two animation resources in.xml and out.xml in res folder

in.xml

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

out.xml

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

Then write in the resources tab of styles.xml under the values folder:

    <resources>

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

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

    <style name="fade" parent="@android:style/Animation.Activity">
        <item name="android:activityOpenEnterAnimation">@anim/in</item>
        <item name="android:activityOpenExitAnimation">@anim/out</item>

        <item name="android:activityCloseEnterAnimation">@anim/in</item>
        <item name="android:activityCloseExitAnimation">@anim/out</item>
    </style>
</resources>

The final step is to add android:theme = "@ style/AppTheme" to the declaration of Activity in Android manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.project.project2test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Main2Activity"
            android:theme="@style/AppTheme"></activity>
    </application>

</manifest>

Posted by klance on Thu, 02 Apr 2020 22:31:47 -0700