1

我想創建一個活動轉換共享元素,如解釋hereAndroid共享元素重新輸入轉換沒有動畫

我希望左側的列表項轉化爲右側突出顯示的框。 this is what I want

那麼,這很好,但不幸的是,退出轉換根本不起作用。從第二個活動回來後,我得到: enter image description here

CardView只是停留在原來的位置,並且不會縮放或轉換任何地方。片刻之後,它消失了。

我認爲退出過渡將是回退過渡的輸入過渡,但在這裏似乎並不是這樣。我怎樣才能讓CardView轉換回左邊的ListItem?

這是我當前的代碼:

在第一個活動的OnItemClickListener

Intent intent = new Intent(context, DisplayEpisodeActivity.class); 
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,clickedRow.findViewById(R.id.background), "background"); 
startActivity(intent, options.toBundle()); 

我的主題:

<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="NewApi"> 
    <style name="BaseTheme" parent="android:Theme.Material.Light.DarkActionBar"> 
     <item name="android:colorPrimary">#D32F2F</item> 
     <item name="android:textColorPrimary">#616161</item> 
     <item name="android:colorPrimaryDark">#B71C1C</item> 
     <item name="android:colorAccent">#757575</item> 
     <item name="android:colorControlHighlight">#EF9A9A</item> 
     <item name="android:windowContentTransitions">true</item> 
     <item name="android:windowSharedElementEnterTransition"> 
      @transition/change_image_transform</item> 
     <item name="android:windowSharedElementExitTransition"> 
      @transition/change_image_transform</item> 
    </style> 
</resources> 

change_image_transform.xml:

<?xml version="1.0" encoding="utf-8"?> 
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"> 
    <changeTransform/> 
    <changeBounds/> 
</transitionSet> 

而且我已經設置了transitionName到右側的佈局android.support.v7.widget.CardView並向RelativeLayout在你的左邊。

+2

當它剛剛淡出時,它是和指示共享元素不被另一個接受側。如果transitionNames在適配器中沒有唯一地分配(或者爲查看持有者的回收視圖),您應該創建SharedElementCallback並覆蓋onMapSharedElements以告知共享元素轉換哪個View是您的共享元素。 –

+0

謝謝@George!覆蓋onMapSharedElements完美工作。 –

+0

謝謝@GeorgeMount!您的評論應該是被接受的答案 – Shine

回答

5

活動和片段共享轉換可能很難調試。默認情況下,動畫速度很快,有時眼睛只能看到「有什麼問題」,但要弄清楚爲什麼可以是不重要的。

在調試從A到B共享過渡,我通常找到重寫onMapSharedElements方法是非常有用的,這樣可以很容易地找出哪些共享所述的元件被關聯到在B.像這樣的元件(內部B的onCreate()方法:

setEnterSharedElementCallback(new SharedElementCallback() { 

      @Override 
      public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) { 
       Log.i(Constants.TAG, "EnterSharedElement.onMapSharedElements:" + sharedElements.size()); 
       //manual override of non-working shared elements goes here 
       super.onMapSharedElements(names, sharedElements); 
      } 

      @Override 
      public void onRejectSharedElements(List<View> rejectedSharedElements) { 
       //Some element was rejected? Aha!! 
       Log.i(Constants.TAG, "EnterSharedElement.onMapSharedElements:" + rejectedSharedElements.size()); 
       super.onRejectSharedElements(rejectedSharedElements); 
      } 

      @Override 
      public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) { 
       //AFTER the shared transition 
       super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots); 
      } 
     }); 
    }catch (Exception uie){ 
     Log.e(Constants.TAG,"UIE:"+uie.getMessage()); 
    } 

通過這種方式,它會更容易找出哪些元素不被映射(也許他們只是簡單地尚未建立),並解決共享過渡問題(即:毛刺,神器)

+0

這很有用。除非日誌全部讀取相同,否則共享元素不會被拒絕。 –

相關問題