在android5.0中增加了一个activity之间的国度动画,效果较之前自己写的好很多,在5.0之前,如果想要实现activity之间的跳转动画,则需要使用activity的 Activity.overridePendingTransition方法,并且自定义一个动画,但是在5.0当中google已经为我们提供了一下动画:
1. Explode
2. Fade
3. Slide
4. ChangeTransform
5. ChangeClipBounds
6. ChangeBounds
1.在setContentView之前,声明允许过度动画:
//设置允许过度动画,一定要在setContentView之前调用 getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
setContentView(R.layout.activity_main);
2.关键代码:
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.one:
getWindow().setExitTransition(new Explode());
intent = new Intent(MainActivityTwo.this,TwoActivity.class);
startActivity(intent,
ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.two:
getWindow().setExitTransition(new ChangeTransform());
intent = new Intent(MainActivityTwo.this,TwoActivity.class);
startActivity(intent,
ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.three:
getWindow().setExitTransition(new ChangeClipBounds());
intent = new Intent(MainActivityTwo.this,TwoActivity.class);
startActivity(intent,
ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.four:
ChangeBounds changeBounds = new ChangeBounds();
getWindow().setExitTransition(changeBounds);
intent = new Intent(MainActivityTwo.this,TwoActivity.class);
startActivity(intent,
ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.five:
getWindow().setExitTransition(new Fade());
intent = new Intent(MainActivityTwo.this,TwoActivity.class);
startActivity(intent,
ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.six:
Slide slide = new Slide();
slide.setDuration(2000); //设置返回到该界面过度的时间
slide.setInterpolator(new DecelerateInterpolator()); //设置返回到该界面的的加速模式
slide.setStartDelay(1000);
getWindow().setExitTransition(slide);
intent = new Intent(MainActivityTwo.this,TwoActivity.class);
startActivity(intent,
ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
default:
}
这里我跳转到的是 TwoActivity这个activity,如果想在改activity退出时同时执行相同的动画,可以在退出的时候执行如下代码:
finishAfterTransition();
即可
效果如下:
版权声明:本文为博主原创文章,未经博主允许不得转载。
android5.0 Activity transition
原文地址:http://blog.csdn.net/mockingbirds/article/details/47058555