首页 > 解决方案 > 如何在屏幕之间共享反应原生组件

问题描述

我正在尝试在两个屏幕之间共享 react native 组件,就像你可以在 ios 中使用hero库一样。我希望组件在两个屏幕之间进行动画处理。

我已经尝试过流体转换库,但我无法让它工作。

IE。

标签: androidiosreact-native

解决方案


为了制作这样的动画,您可以使用 TransitionManager,我个人通常将它与 constraintLayouts 一起使用。

为此,您需要 2 个约束布局(两者都需要具有将出现在屏幕上的所有元素,并且在布局之间更改的属性应该只是约束本身。)

使用 2 种布局,您可以像这样制作从一个到另一个的动画:

final ConstraintSet constraint1 = new ConstraintSet();
constraint1.clone(getApplicationContext(), R.layout.layout_shrunk);

final ConstraintSet constraint2 = new ConstraintSet();
constraint2.clone(getApplicationContext(), R.layout.layout_expanded);

//The view you pass as an argument to both methods should be the layout's root.
TransitionManager.beginDelayedTransition((ConstraintLayout)findViewById(R.id.layout_root));
constraint2.applyTo((ConstraintLayout) findViewById(R.id.layout_root));

结果:

在此处输入图像描述

但是这种动画也可以通过其他方式制作,请查看官方文档以供参考:https ://developer.android.com/training/transitions

注意:要获得反向动画也只需应用其他约束集。


推荐阅读