Actiivty转场动画过去常使用overridePendingTransition (int enterAnim, int exitAnim),现一种新的方式ActivityOptions,和兼容类(V4)ActivityOptionsCompat。
ActivityOptionsCompat包含了多个场景转换静态方法,现详细介绍makeSceneTransitionAnimation。
在启动Activity中:
ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
this,
//平滑的将多个控件平移的过渡到第二个activity
//如下,将两个控件R.id.imageview_item与R.id.textview_name平移
//根据需要,可以平移更多的控件。
new Pair<View, String>(view.findViewById(R.id.imageview_item),
DetailActivity.VIEW_NAME_HEADER_IMAGE),
new Pair<View, String>(view.findViewById(R.id.textview_name),
DetailActivity.VIEW_NAME_HEADER_TITLE));
// Now we can start the Activity, providing the activity options as a bundle
ActivityCompat.startActivity(this, intent, activityOptions.toBundle());
接收的Activity:
//获取本界面中两个对应的布局控件
mHeaderImageView = (ImageView) findViewById(R.id.imageview_header);
mHeaderTitle = (TextView) findViewById(R.id.textview_title);
/**
* 设置被共享的控件,由上一个Activity传入,通过自定义常量标识获取(VIEW_NAME_HEADER_IMAGE)
* Set the name of the view's which will be transition to, using the static values above.
* This could be done in the layout XML, but exposing it via static variables allows easy
* querying from other Activities
**/
ViewCompat.setTransitionName(mHeaderImageView, VIEW_NAME_HEADER_IMAGE);
ViewCompat.setTransitionName(mHeaderTitle, VIEW_NAME_HEADER_TITLE);