属性动画
0.使用例子
// 1. 改变位置
ObjectAnimator.ofFloat(imageView, "translationX", 0f, 300f).start();
// 2. 改变背景颜色
ValueAnimator valueAnimator = ObjectAnimator.ofInt(viewTwo,
"backgroundColor", 0xFFFF0808, 0xFF08FF08);
valueAnimator.setDuration(3000);
valueAnimator.setEvaluator(new ArgbEvaluator());
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
valueAnimator.start();
// 3. AnimatorSet的使用
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
ObjectAnimator.ofFloat(viewThree, "rotationX", 0, 360),
ObjectAnimator.ofFloat(viewThree, "rotationY", 0, 180),
ObjectAnimator.ofFloat(viewThree, "rotation", 0, 90),
ObjectAnimator.ofFloat(viewThree, "translationX", 0, 100),
ObjectAnimator.ofFloat(viewThree, "translationY", 0, 100),
ObjectAnimator.ofFloat(viewThree, "scaleX", 0, 1.5f),
ObjectAnimator.ofFloat(viewThree, "scaleY", 0, 1.0f),
ObjectAnimator.ofFloat(viewThree, "alpha", 1f, 0.75f, 1f)
);
animatorSet.setDuration(5000).start();
//4.两秒内先旋转一圈,再旋转一圈
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f, 0f);
animator.setDuration(2000);
animator.start();
//5.先X轴放大两倍再变回原样
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f, 1f);
animator.setDuration(2000);
animator.start();
1. 补间动画的不足
1.补间动画只能对view进行操作
2.补间动画只能进行平移,缩放,旋转,淡入淡出四种操作
3.补间动画只是改变view的显示效果,而没有改变view的属性(例如一个按钮,通过补间动画移动之后,它其实只是将这个按钮绘制到了新的地方,而点击这个按钮是没有任何反应的,但是点击按钮的原来位置会有点击事件)
2.属性动画的引入
1.属性动画的操作对象不限于view
2.属性动画可以执行多种动画效果,不限于那四种,例如可以改变背景
3.属性动画其实是一种值机制,通过改变对象的属性来实现的动画效果,所以不存在移动之后的问题(按钮是真正的移动了,不仅仅是绘制)
3.常用的类
3.1 ValueAnimator 操作值
ValueAnimator的ofInt和ofFloat方法可以通过int和float类型的属性来同时结合IntEvaluator和FloatEvaluator执行动画,ValueAnimator还有一个ofObject方法,这时就需要自定义TypeEvaluator了。
如:
public class Point {
private float x;
private float y;
public Point(float x, float y) {
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
}
public class PointEvaluator implements TypeEvaluator{
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
Point startPoint = (Point) startValue;
Point endPoint = (Point) endValue;
float x = startPoint.getX() + fraction * (endPoint.getX() - startPoint.getX());
float y = startPoint.getY() + fraction * (endPoint.getY() - startPoint.getY());
Point point = new Point(x, y);
return point;
}
}
Point point1 = new Point(0, 0);
Point point2 = new Point(300, 300);
ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);
anim.setDuration(5000);
anim.start();
3.2 ObjectAnimator 操作属性
ObjectAnimator内部的工作机制是通过寻找特定属性的get和set方法,然后通过方法不断地对值进行改变,从而实现动画效果的
3.3 AnimatorSet 动画集合
ObjectAnimator moveIn = ObjectAnimator.ofFloat(textview, "translationX", -500f, 0f);
ObjectAnimator rotate = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);
ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);
AnimatorSet animSet = new AnimatorSet();
animSet.play(rotate).with(fadeInOut).after(moveIn);//设置动画集合的播放顺序
animSet.setDuration(5000);
animSet.start();
3.4 ViewPropertyAnimator
1.整个ViewPropertyAnimator的功能都是建立在View类新增的animate()方法之上的,这个方法会创建并返回一个ViewPropertyAnimator的实例,之后的调用的所有方法,设置的所有属性都是通过这个实例完成的
2.在使用ViewPropertyAnimator时,我们自始至终没有调用过start()方法,这是因为新的接口中使用了隐式启动动画的功能,只要我们将动画定义完成之后,动画就会自动启动。
//把textview变成透明状态
textview.animate().alpha(0f);
//移动到400,400动画执行3秒
view.animate().x(400).y(400).setDuration(3000);
view.animate().x(300).y(300).setDuration(2000)
.setInterpolator(new BounceInterpolator());
imageView.animate()
.alpha(0.5f)
.y(300)
.setDuration(2000)
//api min is 16
.withStartAction(new Runnable() {
@Override
public void run() {
}
})
//api min is 16
.withEndAction(new Runnable() {
@Override
public void run() {
}
})
.start();
4.监听器的设置
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
4.1 监听器简单版本
anim.addListener(new AnimatorListenerAdapter() {
//需要哪个实现哪个
});
5.通过XML来实现
5.1 XML的编写
1.
对应ValueAnimator
2.对应ObjectAnimator
3.对应AnimatorSet
5.2 加载XML:通过AnimatorInflater来加载
Animator animator = AnimatorInflater.loadAnimator(context, R.animator.anim_file);
animator.setTarget(view);
animator.start();
https://developer.android.com/guide/topics/graphics/prop-animation.html
6.TypeEvaluator估值器 :从开始值到结束值如何过渡
系统内置的有
1.IntEvaluator
2.FloatEvaluator
3.ArgbEvaluator
public class FloatEvaluator implements TypeEvaluator {
//按比例fraction返回一个值
public Float evaluate(float fraction, Number startValue, Number endValue) {
float startFloat = startValue.floatValue();
return startFloat + fraction * (endValue.floatValue() - startFloat);
}
}
7.Interpolator插值器:控制动画的变化速率
系统预置的有
1.LinearInterpolator线性插值器
2.AccelerateDecelerateInterpolator加减速插值器,两头慢中间快
3.DecelerateIntepolator减速插值器,越来越慢
4.BounceIntepolator
public class AccelerateDecelerateInterpolator extends BaseInterpolator
implements NativeInterpolatorFactory {
public AccelerateDecelerateInterpolator() {
}
@SuppressWarnings({ "UnusedDeclaration" })
public AccelerateDecelerateInterpolator(Context context,
AttributeSet attrs) {
}
public float getInterpolation(float input) {
return (float) (Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
}
}
public class LinearInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
public LinearInterpolator() {
}
public LinearInterpolator(Context context, AttributeSet attrs) {
}
public float getInterpolation(float input) {
return input;
}
}
8.PropertyValuesHolder:针对同一个对象多个属性,同时作用多种动画
PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("translationX", 300f);
PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f);
PropertyValuesHolder propertyValuesHolder3 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f);
PropertyValuesHolder propertyValuesHolder4 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f);
ObjectAnimator.ofPropertyValuesHolder(imageView, propertyValuesHolder1,
propertyValuesHolder2, propertyValuesHolder3, propertyValuesHolder4)
.setDuration(5000).start();
参考
http://blog.csdn.net/cxmscb/article/details/52655075
http://blog.csdn.net/cxmscb/article/details/52636918
http://blog.csdn.net/cxmscb/article/details/52685864
http://blog.csdn.net/ahence/article/details/46646559
https://github.com/ASPOOK/Android-AnimationBanner
http://www.jianshu.com/p/03fdcfd3ae9c