Material Design设计语言动画篇共推出六种类型的动画效果:
1、Touch feedback(触摸反馈)
2、Reveal effect(揭露效果)
3、Activity transitions(Activity转换效果)
4、Curved motion(曲线运动)
5、View state changes (视图状态改变)
6、Animate Vector Drawables(矢量动画)
安卓5.0后提供了一个过度动画框架,而且transition框架是基于属性 动画实现的,transition框架是对属性动
画的一种封装,transition框架提供的api可以简单的实现很复杂的属性动画效果,
一 **转场动画**
xml定义方式
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<explode android:duration="2000"/>
</transitionSet>
记得style.xml文件中加入
<itemname="android:windowEnterTransition">@transition/activity_explode.xml</item>
2代码中实现方式
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
也可以在style.xml文件中加入
<item name="android:windowEnterTransition">true</item>
主Activity
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.threelayout);
getWindow().setEnterTransition(new Explode().setDuration(2000));
getWindow().setEnterTransition(new Fade().setDuration(2000));
getWindow().setExitTransition(new Slide().setDuration(2000));
如果没有设置将执行进入或者退出的反向动画效果
getWindow().setEnterTransition(new Fade().setDuration(5000));
getWindow().setReenterTransition(new Fade().setDuration(2000));
}
Intent intent4 = new Intent(MainActivity.this, DetialActivity.class);
startActivity(intent4, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
在第二个activity中可以设置进入的动画和返回的动画效果
getWindow().setEnterTransition(new Explode().setDuration(2000));
getWindow().setReenterTransition(new Fade().setDuration(2000));
**2.Activity间的共享元素**
Transition框架知道用适当的动画向用户展示从一个view向另外
一个view过度。
设置共享属性名字 两个共享view设置一样
android:transitionName="mybutton"
Intent intent3 = new Intent(MainActivity.this, ThreeActivity.class);
startActivity(intent3, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this,
button3, "mybutton").toBundle());
多个共享属性设置方式
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,
Pair.create(view1, "mybutton"),
Pair.create(view2, "mybutton"));
二*Reveal effect(揭露效果)**
view - 需要显示揭露动画的View
centerX - 动画开始的X轴坐标
centerY - 动画开始的Y轴坐标
startRadius - 动画开始时的半径
endRadius - 动画结束时的半径
Animator animator = ViewAnimationUtils.createCircularReveal(button1, button1.getWidth() / 2, button1.getHeight() / 2, 0, button1.getHeight());
animator.setDuration(500);
animator.setInterpolator(new AccelerateInterpolator());
animator.start();
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Intent intent5 = new Intent(MainActivity.this, FvieActivity.class);
startActivity(intent5);
}
});
float hypot = (float) Math.hypot(button5.getWidth(), button5.getHeight());
Animator animator1 = ViewAnimationUtils.createCircularReveal(button5, button5.getWidth(), 0, 0, hypot);
animator1.setDuration(500);
animator1.setInterpolator(new AccelerateInterpolator());
animator1.start();
animator1.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Intent intent5 = new Intent(MainActivity.this, SixActivity.class);
startActivity(intent5);
}
});
**三,扩张动画**
/**
*@source - The View that the new activity is animating from,界面是从哪个视图上开始扩张
*@startX - 开始扩张的x坐标
*@startY - 开始扩张的y坐标
*@startWidth - 扩张完后Activity的宽度(为0表示全屏)
*@startHeight - 扩张完后Activity的高度(为0表示全屏)
*/
Intent intent6 = new Intent(MainActivity.this, SevenActivity.class);
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeScaleUpAnimation(button6, button6.getWidth()/2, button6.getHeight()/2, 0, 0);
startActivity(intent6, optionsCompat.toBundle())
返回时调用
@Override
protected void onDestroy() {
super.onDestroy();
finishAfterTransition();
}