Android5.0之前要修改页面转场动画,只能去写一些view动画,然后通过overridePendingTransition方法设置进去。Android5.0之后Activity的出入场动画总体上来说可以分为两种,一种就是分解、滑动进入、淡入淡出,另外一种就是共享元素动画,
1共享元素转场动画
这是什么意思了?ActivityA的界面有一个ImageView’并为它设置了背景图片,现在我们要跳转到ActivityB界面,B界面里同样有一个imageView图片,也需要用到A界面的背景图片,这个时候共享元素转场动画就有用武之地了,我们来看看这个效果
这个效果怎么做的了,当点击了列表中的图片就跳转到另一个界面上去了,而且还有动画的层次感,这个页比较简单
//Activity共享元素转场动画
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(ToolbarActivity.this, view.findViewById(R.id.iv_icon),"basic");
intent = new Intent(ToolbarActivity.this, MovieDetailActivity.class);
intent.putExtra("URL", movie.get(position).getImages().getMedium());
intent.putExtra("NAME", movie.get(position).getTitle());
startActivity(intent, optionsCompat.toBundle());
最关键的就是这行代码了
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(ToolbarActivity.this, view.findViewById(R.id.iv_icon),"basic");
xml界面属性 两个界面要共享的view都需要加上:
android:transitionName="名字自己取"
例如
第一个Activity的xml布局,需要共享的元素,加上transitionName属性。至于名字,你随便定义,要保持相同
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom"
tools:context="com.haichenyi.activitytransitionanimation.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="300dp"
android:layout_height="200dp"
android:transitionName="basic"//加上transitionName属性
android:src="@mipmap/bg"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转"/>
</LinearLayout>
<!--第二个activity的xml布局-->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@mipmap/bg"
android:transitionName="basic"/>//同样加上transitionName属性,属性名字相同
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
</android.support.constraint.ConstraintLayout>
Activity
//共享元素转场动画,只支持android 5.0,所以加一个判断
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startActivity(new Intent(MainActivity.this, Step1Activity.class),
ActivityOptions.makeSceneTransitionAnimation(MainActivity.this,
findViewById(R.id.img), "basic").toBundle());
} else {
startActivity(new Intent(MainActivity.this, Step1Activity.class));
}
这样就可以实现共享元素转场动画的效果了
2分解、滑动进入、淡入淡出 动画
我们这里演示的是分解动画效果,至于其它的效果大家可以去试试
实现这个动画效果很简单
step1 第一个activity
//过度动画
Intent intent = new Intent(this, VipActivity.class);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
step2 第二个activity
// 允许使用transitions
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
//淡入动画
//getWindow().setEnterTransition(new Fade());
//滑动动画
//getWindow().setEnterTransition(new Slide());
// 分解动画
getWindow().setEnterTransition(new Explode());
就以上两个步骤 注意的是这行代码,需要在oncreate()方法的setconentView()之前加上
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
也可以在style文件中使用 这两种方式都可以
<item name="android:windowContentTransitions">true</item>
// 允许使用transitions 才会有效果