Property Animation in Honeycomb

http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html

 

1.使用ValueAnimator

在回调函数中执行动画操作

ValueAnimatoranim=ValueAnimator.ofFloat(0f,1f);

// ValueAnimatoranim=ValueAnimator.ofInt(0,100);
anim.setDuration(500);

anim.addUpdateListener(newValueAnimator.AnimatorUpdateListener(){
       publicvoidonAnimationUpdate(ValueAnimatoranimation){
           Floatvalue=(Float)animation.getAnimatedValue();
           // do something with value…
       }
   });
anim.start();

—–

publicclassPointEvaluatorimplementsTypeEvaluator{
       publicObjectevaluate(floatfraction,ObjectstartValue,ObjectendValue){
           PointstartPoint=(Point)startValue;
           PointendPoint=(Point)endValue;
           returnnewPoint(startPoint.x+fraction(endPoint.x-startPoint.x),
                startPoint.y+fraction
(endPoint.y-startPoint.y));
       }
   }

Pointp0=newPoint(0,0);
Pointp1=newPoint(100,200);
ValueAnimatoranim=ValueAnimator.ofObject(newPointEvaluator(),p0,p1);


写在xml中:

<animatorxmlns:android="http://schemas.android.com/apk/res/android&quot;
       android:valueFrom="0"
       android:valueTo="100"
       android:valueType="intType"/>


2.ObjectAnimator

ObjectAnimator.ofFloat(myObject,"alpha",0f).start();

或者写在xml中

<objectAnimatorxmlns:android="http://schemas.android.com/apk/res/android&quot;
       android:valueTo="0"
       android:propertyName="alpha"/>

ObjectAnimatoranim=AnimatorInflator.loadAnimator(context,resID);
anim.setTarget(myObject);
anim.start();

对于ObjectAnimator,必须有该property的get和set函数,会在运行时通过reflection调用:

publicvoidsetAlpha(floatvalue);
publicfloatgetAlpha();


3.AnimatorSet

ObjectAnimatorfadeOut=ObjectAnimator.ofFloat(v1,"alpha",0f);
ObjectAnimatormover=ObjectAnimator.ofFloat(v2,"translationX",-500f,0f);
ObjectAnimatorfadeIn=ObjectAnimator.ofFloat(v2,"alpha",0f,1f);
AnimatorSetanimSet=newAnimatorSet().play(mover).with(fadeIn).after(fadeOut);;
animSet.start();

 

4.ViewPropertyAnimator

myView.animate().x(50f).y(100f);  (animate() returns an instance of ViewPropertyAnimator)

等价==》

PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);

PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);

ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();

比AnimatorSet效率高==》

ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);

ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);

AnimatorSet animSetXY = new AnimatorSet();

animSetXY.playTogether(animX, animY);

animSetXY.start();