2016-07-02 139 views
0

我想在TitleScreenActivity和MainActivity中離開時在android中播放transitionSet。問題是:它只播放淡入淡出的動畫,但不播放其他兩張幻燈片。Android轉換不起作用

這裏是我的代碼:

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()); 

回答

1

我實際上已經開始工作了。我只是從我的MainActivity的CoordinatorLayout中刪除了標籤

android:background="@color/colorPrimary" 

。我不知道爲什麼,但它現在起作用。

+2

或者,您可以保留背景並指定'android:transitionGroup =「false」'。 – keyboardsurfer

1

爲了使用窗口活動轉換,您必須使用windowActivityTransitions標誌。您也可以從Theme.AppCompat繼承API 21+繼承自Theme.Material

您一直在使用的標誌windowContentTransitions告訴系統您打算使用TransitionManager在場景之間切換。

+0

我的主題確實繼承自Theme.AppCompat,我用windowActivityTransitions替換了windowContentTransition標誌。但是,佈局仍然不滑。 –

+0

您是否找到了解決方案? – Andrea

+0

是@Andrea。檢查[我的回答](https://stackoverflow.com/a/38188955/5861618) –