以前我们定义activity转场动画都是如下:
Intent intent=new Intent(this,SecondActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim);
先看下内置的3种动画效果吧
是不是看起来很炫酷,老实说我没感觉有啥炫酷的,哈哈
这3种动画分别是 explode、slide、fade
现在来学习下怎么使用的吧
前置任务:
在setContentView()之前执行,用于告诉Window页面切换需要使用动画
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
或者在主题里面添加
- true
A Activity中代码:
switch (view.getId()) {
case R.id.tv_explode_transition:
mIntent.setClass(this, TransitionActivity.class);
mIntent.putExtra("transition", "explode");
startActivity(mIntent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.tv_slide_transition:
mIntent.setClass(this, TransitionActivity.class);
mIntent.putExtra("transition", "slide");
startActivity(mIntent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.tv_fade_transition:
mIntent.setClass(this, TransitionActivity.class);
mIntent.putExtra("transition", "fade");
startActivity(mIntent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
switch (transition) {
case "explode":
imageView.setBackgroundResource(R.drawable.circle_red);
Explode explode1 = new Explode();
getWindow().setEnterTransition(explode1);
break;
case "slide":
imageView.setBackgroundResource(R.drawable.circle_purple);
Slide slide = new Slide(Gravity.BOTTOM);
slide.setDuration(500L);
getWindow().setEnterTransition(slide);
break;
case "fade":
imageView.setBackgroundResource(R.drawable.circle_blue);
Fade fade = new Fade();
fade.setDuration(1000L);
getWindow().setEnterTransition(fade);
break;
}
这里还有其他使用方法,效果一样,用XML文件来实现,如图:
getWindow().setExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.slide));
getWindow().setEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.fade));