我试图使用转换API来为两个ViewGroups之间的共享元素设置动画效果。目标是绿色观点“走出其父母的界限”走向新的位置。使用嵌套共享元素的场景转换
我有以下布局:
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>
但是,我不能得到这个好好工作。默认转换只是淡化了所有内容,ChangeBounds
转换完全没有效果,ChangeTransform
也不正确。
我正在使用的代码:
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);
}
现在我可以手动通过使用ViewOverlay
创建动画自己做到这一点。不过,我正在使用Transitions API寻找解决方案。这甚至有可能吗?
此外,我不希望'扁平'的层次结构。我故意嵌套视图来解决更复杂的用例。
虽然这可能是这对你的作品的解决方案,我建议考虑进去一看我的回答并考虑去的Android转换API的用途。 – keyboardsurfer