我的应用中有一些 CardView,我希望它们能够像 Google Keep 中的卡片一样运行。例如,当我单击包含文本的卡片时,它会(通过动画)扩展为另一个视图。
如果您不确定我的意思,请在 Google Keep Android 应用上创建一个便笺,点击创建便笺时出现的卡片。这正是我希望在我的应用程序中发生的事情。
我该怎么做呢?
我的应用中有一些 CardView,我希望它们能够像 Google Keep 中的卡片一样运行。例如,当我单击包含文本的卡片时,它会(通过动画)扩展为另一个视图。
如果您不确定我的意思,请在 Google Keep Android 应用上创建一个便笺,点击创建便笺时出现的卡片。这正是我希望在我的应用程序中发生的事情。
我该怎么做呢?
棒棒糖的新功能!
通过声明跨两个屏幕通用的“共享元素”,您可以在两种状态之间创建平滑过渡。
相册_grid.xml:
<ImageView
…
android:transitionName="@string/transition_album_cover" />
专辑详细信息.xml:
<ImageView
…
android:transitionName="@string/transition_album_cover" />
爪哇:
AlbumActivity.java
Intent intent = new Intent();
String transitionName = getString(R.string.transition_album_cover);
…
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
albumCoverImageView, // The view which starts the transition
transitionName // The transitionName of the view we’re transitioning to
);
ActivityCompat.startActivity(activity, intent, options.toBundle());
这里我们在两个屏幕中定义相同的transitionName。启动新 Activity 时,此转换会自动设置动画。除了共享元素之外,您现在还可以编排进入和退出元素。