当在一个共享的元素活动过渡动画嵌套的看法故障?嵌套、故障、元素、看法

2023-09-05 10:40:10 作者:隐身守侯

我一直插科打诨与新的API中的Andr​​oid 5.0,并一直在试图找出是否有可能进行动画既是的ViewGroup 和一其子女分别为活动过渡过程中共享的元素。

I've been messing around with the new APIs in Android 5.0 and have been trying to figure out whether or not it is possible to animate both a ViewGroup and one of its children separately as shared elements during an Activity transition.

下面的截图给出了什么,我想实现一个简单的例子:

The screenshots below gives a simplified example of what I am trying to achieve:

在第一个活动,暗灰色方块是一个的ViewGroup 集中在屏幕上,红色框是其子查看(布局XML code,我使用可以发现here).当用户点击深灰色框,深灰色框应逐步扩大规模,以填补第二项活动的背景。同时,红色框应逐渐形成规模,并祭出了第二个活动的左上角。

In the first activity, the dark gray box is a ViewGroup centered in the screen and the red box is its child View (the layout XML code I am using can be found here). When the user clicks on the dark gray box, the dark gray box should gradually scale up to fill the second activity's background. At the same time, the red box should gradually scale and reposition itself in the top left corner of the second activity.

活动 code我使用执行的过渡是简单的:

The Activity code I am using to perform the transition is simple:

/** FirstActivity.java */
public class FirstActivity extends Activity implements View.OnClickListener {
    private View mOuterBox, mInnerBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        getWindow().setSharedElementExitTransition(new ChangeBounds());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        mOuterBox = findViewById(R.id.outer_box);
        mInnerBox = findViewById(R.id.inner_box);
        mOuterBox.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Pair<View, String> outerBox = Pair.create(mOuterBox, mOuterBox.getTransitionName());
        Pair<View, String> innerBox = Pair.create(mInnerBox, mInnerBox.getTransitionName());
        Bundle bundle = ActivityOptions.makeSceneTransitionAnimation(this, outerBox, innerBox).toBundle();
        startActivity(new Intent(this, SecondActivity.class), bundle);
    }
}

/** SecondActivity.java */
public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        getWindow().setSharedElementEnterTransition(new ChangeBounds());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}

问题

当我在第一个活动点击深灰色框,共享的元素开始过渡和暗灰色的框可扩展至很好地填补了第二个活动的背景。 然而,红色框不会出现在所有的动画。一旦过渡开始​​,红盒子突然调整大小和位置本身的灰色框前的第二次活动在其最终位置甚至已经完成的动画。的

是否有可能进行动画既是的ViewGroup 和一个/一些/所有的孩子意见独立共享在的活动内容过渡?如果是的话,我究竟做错了,我能做些什么,以确保意见动画呢?

Is it possible to animate both a ViewGroup and one/some/all of its children views independently as shared elements in an Activity transition? If so, what am I doing wrong and what can I do to ensure the children views are animated as well?

如果您遇到了麻烦,下面我的描述,完整的源$ C ​​$ C这个示例项目可在 GitHub的 和一个可运行的APK可供下载here (你需要一个物理设备或模拟器运行Android 5.0运行APK)。

If you had trouble following my descriptions, the full source code for this example project is available on GitHub and a runnable APK is available for download here (you'll need a physical device or emulator running Android 5.0 to run the APK).

推荐答案

是的,你可以分别转换一组及其内容。但你发现,将被固定L中MR1的错误。

Yes, you can transition a group and its contents separately. But you just found a bug that will be fixed in L MR1.

看来,这不是很好的支持L.我建议您在第二个活动的共享元素的兄弟姐妹:

It appears that this is not well supported in L. I recommend that you make the shared elements siblings in the second Activity:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/dark_gray"
        android:transitionName="outer_box"/>

    <RelativeLayout
        android:id="@+id/outer_box"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:id="@+id/inner_box"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:background="@color/red"
            android:transitionName="inner_box" />
    </RelativeLayout>
</FrameLayout>
 
精彩推荐
图片推荐