Android 转换无效

Android Transitions not working

当我从 TitleScreenActivity 离开到 MainActivity 时,我试图在 android 中播放一个 transitionSet。问题是:它只播放淡入淡出动画,但不播放其他2张幻灯片。

这是我的代码:

transition_menu.xml

<?xml version="1.0" encoding="utf-8"?>
        <transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
            android:transitionOrdering="together"
            android:duration="500"
            >
            <fade xmlns:android="http://schemas.android.com/apk/res/android">
                <targets>
                    <target android:excludeId="@android:id/statusBarBackground"/>
                    <target android:excludeId="@android:id/navigationBarBackground"/>
                </targets>
            </fade>

            <slide android:slideEdge="top">
                <targets>
                    <target android:targetId="@id/toolbar"/>
                </targets>
            </slide>

            <slide android:slideEdge="bottom">
                <targets>
                    <target android:targetId="@id/bottomSheet"/>
                </targets>
            </slide>
        </transitionSet>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context="com.rpfsoftwares.multitap.MainActivity">

        <include layout="@layout/custom_toolbar"/>

        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/viewPager"
            android:elevation="0dp"
            android:layout_marginTop="96dp"
            android:layout_marginBottom="148dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/bottomSheet"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:background="@color/colorWhite"
            android:weightSum="1"
            android:paddingTop="24dp"
            android:paddingBottom="24dp"
            android:layout_gravity="bottom|center_horizontal"
            android:elevation="4dp"
            android:layout_height="148dp">

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:weightSum="0.9"
                android:id="@+id/bottomTxt"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:layout_weight="0.5"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_gravity="center_vertical"
                    android:text="@string/bestScore"
                    android:textAlignment="center"
                    android:layout_weight="0.3" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_gravity="center"
                    android:text="@string/totalTaps"
                    android:textAlignment="center"
                    android:layout_weight="0.3" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_gravity="center"
                    android:text="@string/totalTime"
                    android:textAlignment="center"
                    android:layout_weight="0.3" />
            </LinearLayout>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:weightSum="1.8"
                android:id="@+id/bottom"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:layout_weight="0.5"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_weight="0.3"
                    android:src="@drawable/ic_blur_circular_grey_24dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_gravity="center_vertical"
                    android:text="8031 pts"
                    android:id="@+id/txtPts"
                    android:layout_weight="0.3" />

                <ImageView
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_weight="0.3"
                    android:src="@drawable/ic_touch_app_grey_24dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_gravity="center_vertical"
                    android:text="11 125"
                    android:id="@+id/txtTotalTaps"
                    android:layout_weight="0.3" />

                <ImageView
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_weight="0.3"
                    android:src="@drawable/ic_timer_grey_24dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_gravity="center_vertical"
                    android:text="2h 21min"
                    android:id="@+id/txtTotalTime"
                    android:layout_weight="0.3" />
            </LinearLayout>
        </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="0dp"
        android:transitionName="shared"
        app:layout_anchor="@id/bottomSheet"
        app:layout_anchorGravity="top|center_horizontal"
        app:backgroundTint="@color/colorCyan"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_play_arrow_grey_24dp"
        android:elevation="6dp"
        android:transitionName="shared"
        app:layout_anchor="@id/bottomSheet"
        app:layout_anchorGravity="top|center_horizontal"
        app:backgroundTint="@color/colorWhite"/>


</android.support.design.widget.CoordinatorLayout>

custom_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:weightSum="1"
    android:id="@+id/toolbar"
    android:elevation="4dp"
    android:background="@color/colorWhite"
    android:layout_height="96dp">


    <ImageView
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:padding="32dp"
        android:layout_weight="0.25"
        android:layout_gravity="center_vertical"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:src="@drawable/ic_arrow_back_grey_24dp"
        android:id="@+id/btnBack" />


    <ImageView
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:padding="32dp"
        android:layout_weight="0.25"
        android:layout_gravity="center_vertical"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:src="@drawable/ic_info_outline_grey_24dp"
        android:id="@+id/btnInfo" />


    <ImageView
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:padding="32dp"
        android:layout_weight="0.25"
        android:layout_gravity="center_vertical"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:src="@drawable/ic_share_grey_24dp"
        android:id="@+id/btnShare" />


    <ImageView
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:padding="32dp"
        android:layout_weight="0.25"
        android:layout_gravity="center_vertical"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:src="@drawable/ic_volume_up_grey_24dp"
        android:id="@+id/btnSound" />
</LinearLayout>

activity_title_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.rpfsoftwares.multitap.TitleScreenActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:id="@+id/linearLayout"
        android:background="@color/colorBackground"
        android:layout_height="match_parent">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="#Welcome"
            android:id="@+id/txtAppName"
            android:layout_gravity="center_horizontal" />

        <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_play_arrow_grey_24dp"
        android:elevation="6dp"
        android:transitionName="shared"
        app:backgroundTint="@color/colorCyan" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="tap to start"
            android:id="@+id/txtTitle"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

styles.xml

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>

        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowEnterTransition">@transition/transition_menu</item>
        <item name="android:windowExitTransition">@transition/transition_menu</item>

    </style>

TitleScreenActivity.java

Intent i = new Intent(TitleScreenActivity.this, MainActivity.class);
                ActivityOptionsCompat activityOptions=
                ActivityOptionsCompat.makeSceneTransitionAnimation(TitleScreenActivity.this);
                ActivityCompat.startActivity(TitleScreenActivity.this,i,activityOptions.toBundle());

为了使用 window activity 转换,您必须使用 windowActivityTransitions 标志。您还可以继承自 Theme.AppCompat,后者在 API 21+ 继承自 Theme.Material

您一直在使用的标志 windowContentTransitions 告诉系统您打算使用 TransitionManager 在场景之间进行转换。

我真的让它工作了。我只是删除了标签

android:background="@color/colorPrimary"

来自我的 MainActivity 的 CoordinatorLayout。我不知道为什么,但现在可以了。