I am trying to use the transition APIs to animate a common element between two groups of views. The goal is that the green view moves "from the parent" to a new position.

I have the following layouts:
first.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="200dp" android:layout_height="200dp" android:background="#f00" /> <FrameLayout android:layout_width="200dp" android:layout_height="200dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="#00f"> <View android:id="@+id/myview" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:background="#0f0" /> </FrameLayout> </RelativeLayout>
second.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="200dp" android:layout_height="200dp" android:background="#f00"> <View android:id="@+id/myview" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:background="#0f0" /> </FrameLayout> <FrameLayout android:layout_width="200dp" android:layout_height="200dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="#00f" /> </RelativeLayout>
However, I cannot get this to work correctly. By default, the transition simply disappears, and the ChangeBounds transition ChangeBounds nothing, and ChangeTransform doesn't look right either.
The code I'm using is:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ViewGroup root = (ViewGroup) findViewById(android.R.id.content); setContentView(R.layout.first); View myView1 = findViewById(R.id.myview); myView1.setTransitionName("MYVIEW"); new Handler().postDelayed(new Runnable() { @Override public void run() { View second = LayoutInflater.from(MainActivity.this).inflate(R.layout.second, root, false); View myView2 = second.findViewById(R.id.myview); myView2.setTransitionName("MYVIEW"); Scene scene = new Scene(root, second); Transition transition = new ChangeTransform(); transition.addTarget("MYVIEW"); transition.setDuration(3000); TransitionManager.go(scene, transition); } }, 2000); }
Now I can do it manually by creating the animation myself using ViewOverlay . However, I am looking for a solution using the transition API. Is it possible?
In addition, I do not want to โsmooth outโ the hierarchy. I intentionally insert a View to accommodate more complex use cases.
source share