Material Design 中的动画利用曲线实现时间内插与空间移动模式。 利用 Android 5.0(API 级别 21)及更高版本,您可为动画定义定制时间曲线以及曲线运动模式。
PathInterpolator 类别是一个基于贝塞尔曲线或 Path 对象的全新插入器。 此插入器在一个 1x1 的正方形内指定一个运动曲线,定位点位于 (0,0) 以及 (1,1),而控制点则使用构造函数参数指定。 您也可以将路径插入器定义为一个 XML 资源:
系统将为 Material Design 规范中的三种基本曲线提供 XML 资源:
@interpolator/fast_out_linear_in.xml
@interpolator/fast_out_slow_in.xml
@interpolator/linear_out_slow_in.xml
您可以将一个 PathInterpolator 对象传递给 Animator.setInterpolator() 方法。
ObjectAnimator 类别拥有新的构造函数,可让您一次使用两个或更多属性在路径上为坐标添加动画。 例如,下列动画使用 Path 对象为视图的 X 和 Y 属性添加动画:
ObjectAnimator mAnimator;
mAnimator = ObjectAnimator.ofFloat(view, View.X, View.Y, path);
...
mAnimator.start();
实例A曲线滑动:
Path path = new Path();
path.moveTo(100, 100);
path.quadTo(1000, 300, 300, 700);
ObjectAnimator animator = ObjectAnimator.ofFloat(circleImageView, View.X, View.Y, path);
animator.setDuration(3000);
animator.start();
实例B,平移
Path path = new Path();
path.cubicTo(0.2f, 0f, 0.1f, 1f, 0.5f, 1f);
path.lineTo(1f, 1f);
ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, 500);
animator.setInterpolator(PathInterpolatorCompat.create(path));
animator.start();
Android官网相关地址:https://developer.android.com/training/material/animations.html#CurvedMotion
运行效果,可以下载我在gitHub上的demo: https://github.com/xianjuren/AndroidMaterialDesignAnimation