1,使用ActivityOptionsCompat.makeSceneTransitionAnimation(Activity activity, Pair<View, String>... sharedElements)工厂方法创建一个android.app.ActivityOptions实例
Pair<View, String>... sharedElements参数包含了我们想要过渡的起始View,在被加载Activity中的目标View
例:new Pair<View, String>(view.findViewById(R.id.imageview_item), DetailActivity.VIEW_NAME_HEADER_IMAGE)
2,使用ActivityCompat.startActivity(Activity activity, Intent intent, @Nullable Bundle options)去启动Activity
3,在被加载的Activity中,使用setTransitionName(View view, String transitionName)中使用静态的值设置将要过渡的view的名字,这可以在布局文件做,但是通过静态变量暴露它将使其他类容易查找到它,注意此处的transitionName一定要和Pair对象的第二个参数对应上
4,可以增加TransitionListener监听器,监听过渡动画的执行情况
4.1 获取Transition transition = getWindow().getSharedElementEnterTransition();
4.2
transition.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionEnd(Transition transition) {
// Make sure we remove ourselves as a listener
transition.removeListener(this); }
@Override
public void onTransitionStart(Transition transition) { // No-op}
@Override
public void onTransitionCancel(Transition transition) {
// Make sure we remove ourselves as a listener
transition.removeListener(this); }
@Override
public void onTransitionPause(Transition transition) { // No-op}
@Override
public void onTransitionResume(Transition transition) {// No-op}
});
具体详见google示例