1.属性动画的定义及优点:
属性动画是真实地改变控件的属性,view动画是个假象,平移以后的原来位置还是可以点击的。
2.介绍下补间动画的缺点
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
iv.startAnimation(animation);
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="50%p"
android:fromYDelta="0"
android:toYDelta="50%p"
android:fillAfter="true"
android:duration="500">
</translate>
1).动画是假象,原来 位置可以点击
3.属性动画的几种写法:
(1).直接采用ObjectAnimator方式
1 ObjectAnimator oaX = ObjectAnimator.ofFloat(iv, "translationX", 0f, 200f);
2 oaX.setDuration(500);
3 oaX.start();
效果图:
(2).采用ValueAnimator的方式,ObjectAnimator是继承ValueAnimator的
//2)方法2:用ValueAnimator
ValueAnimator animator1 = ValueAnimator.ofFloat(0f, 100f);
animator1.setDuration(200);
animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//动画在执行的过程当中,不断地调用此方法
// animation.getAnimatedFraction()//百分比
//得到duration时间内 values当中的某一个中间值。0f~100f
float value = (float) animation.getAnimatedValue();//
iv.setScaleX(0.5f + value / 200);//0.5~1
iv.setScaleY(0.5f + value / 200);//0.5~1
}
});
animator1.start();
效果图:
(3).采用PropertyValuesHolder的形式
1 //float... values:代表关键帧的值
2 PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.7f, 1f);
3 PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.7f, 1f);
4 PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.7f, 1f);
5
6 ObjectAnimator animator2 = ObjectAnimator.ofPropertyValuesHolder(iv, holder1, holder2, holder3);
7 animator2.setDuration(1000);
8 animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
9
10 @Override
11 public void onAnimationUpdate(ValueAnimator animation) {
12 // TODO Auto-generated method stub
13 float animatedValue = (float) animation.getAnimatedValue();
14 float animatedFraction = animation.getAnimatedFraction();
15 long playTime = animation.getCurrentPlayTime();
16 System.out.println("animatedValue:" + animatedValue + ", playTime:" + playTime);
17 }
18 });
19 animator2.start();
System.out: animatedValue:1.0, playTime:0
System.out: animatedValue:0.7, playTime:501
animatedValue:1.0, playTime:1002
上面的ofFloat 1f,0.7f,1f是三个关键帧,他们是平分时间,如上面打印所示
4.动画集合AnimatorSet的使用
//动画集合
ObjectAnimator ani1 = ObjectAnimator.ofFloat(iv, "alpha", 1f, 0.7f, 1f);
ObjectAnimator ani2 = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 0.7f, 1f);
ObjectAnimator ani3 = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 0.7f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(500);
// animatorSet.play(anim);//执行当个动画
// animatorSet.playTogether(animator1,animator2,animator3);//同时执行
animatorSet.playSequentially(ani1, ani2, ani3);//依次执行动画
animatorSet.start();
5.动画估值器的使用,实例一个抛物线轨迹
//5.-------------------案例:实现抛物线效果--------------------------
/**
* x:匀速
* y:加速度 y=1/2*g*t*t
* 使用估值器最好实现。
*/
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setDuration(4000);
//TODO:这个一定要设置,定义值类型
valueAnimator.setObjectValues(new PointF(800,0));//其实这两种类型效果一样
// valueAnimator.setObjectValues(new PointF(0,0));
//估值器---定义计算规则
valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
@Override
public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
//s=v*t
PointF p = new PointF();
p.x = 100f * (fraction * 4);
// p.y = 0.5f * 0.98f * (fraction * 4) * (fraction * 4);
p.y = 0.5f * 120f * (fraction * 4) * (fraction * 4);
return p;
}
});
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF pf = (PointF) animation.getAnimatedValue();
iv.setTranslationX(pf.x);
iv.setTranslationY(pf.y);
}
});
valueAnimator.start();
注意点:
1).估值器主要使用定义计算规则
2).valueAnimator.setObjectValues(new PointF(0,0));用于设置变化值的类型
3).PointF pf = (PointF) animation.getAnimatedValue();估值器中返回的值,在动画更新监听中可以不断获取到
效果图: