本文主要源码参考的是Hyman的博客http://blog.csdn.net/lmj623565791/article/details/38067475,支持原创,再次感谢hyman给予的帮助
1.概况
- 3.0之后引入,动画本质是的过程是反射出属性值,接着改变它,然后刷新界面
- 区别View Animation(改变的只是绘制效果,真正的View属性是没有变化的,比如按键大小...)
- 区别Drawable Animation(帧动画)
- 参考Hyman的博客:http://blog.csdn.net/lmj623565791/article/details/38067475
继承结构是:
Animation
|
ValueAnimation
|
ObjectAnimation
其中
- Animation中申明了一些运行动画和停止动画,还有AnimatorListener(接收animaton运行时的运动事件(onStart,onEnd,onCancle,onRepeat)的回调接口)
- ValueAnimation 提供一个计时器计算动画值,还提供AnimatorUpdateListtener--提供动画属性值改变时的回调接口,和ObjectAnimation唯一的区别是不提供作用对象(targetObject)
提供一个
setEvaluator
(TypeEvaluator value)方法设置计时器,
- ObjectAnimation 提供改变对象属性的动画
2.通过ValueAnimation实现动画
好处:不需要操作的对象的属性一定要有getter和setter方法,你可以自己根据当前动画的计算值,来操作任何属性
- public void verticalRun(View view)
- {
- ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight
- - mBlueBall.getHeight());
- animator.setTarget(mBlueBall);
- animator.setDuration(1000).start();
- }
由于是没有设置需要运动的对象,需要设置addUpdateListener监听,如下:
- /**
- * 自由落体
- * @param view
- */
- public void verticalRun( View view)
- {
- ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight
- - mBlueBall.getHeight());
- animator.setTarget(mBlueBall);
- animator.setDuration(1000).start();
- // animator.setInterpolator(value)
- animator.addUpdateListener(new AnimatorUpdateListener()
- {
- @Override
- public void onAnimationUpdate(ValueAnimator animation)
- {
- mBlueBall.setTranslationY((Float) animation.getAnimatedValue());
- }
- });
- }
抛物线:
- /**
- * 抛物线
- * @param view
- */
- public void paowuxian(View view)
- {
- ValueAnimator valueAnimator = new ValueAnimator();
- valueAnimator.setDuration(3000);
- valueAnimator.setObjectValues(new PointF(0, 0));
- valueAnimator.setInterpolator(new LinearInterpolator());
- valueAnimator.setEvaluator(new TypeEvaluator<PointF>()
- {
- // fraction = t / duration
- @Override
- public PointF evaluate(float fraction, PointF startValue,
- PointF endValue)
- {
- Log.e(TAG, fraction * 3 + "");
- // x方向200px/s ,则y方向0.5 * 10 * t
- PointF point = new PointF();
- point.x = 200 * fraction * 3;
- point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
- return point;
- }
- });
- valueAnimator.start();
- valueAnimator.addUpdateListener(new AnimatorUpdateListener()
- {
- @Override
- public void onAnimationUpdate(ValueAnimator animation)
- {
- PointF point = (PointF) animation.getAnimatedValue();
- mBlueBall.setX(point.x);
- mBlueBall.setY(point.y);
- }
- });
3.ObjectAnimation实现动画
比较简单
- package com.example.zhy_property_animation;
- import android.animation.ObjectAnimator;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- public class ObjectAnimActivity extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.xml_for_anim);
- }
- public void rotateyAnimRun(View view)
- {
- ObjectAnimator//
- .ofFloat(view, "rotationX", 0.0F, 360.0F)//
- .setDuration(500)//
- .start();
- }
- }
4.PropertyValuesHolder一个动画实现多个效果
- public void propertyValuesHolder(View view)
- {
- PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
- 0f, 1f);
- PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,
- 0, 1f);
- PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,
- 0, 1f);
- ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();
- }
5、监听动画的事件
对于动画,一般都是一些辅助效果,比如我要删除个元素,我可能希望是个淡出的效果,但是最终还是要删掉,并不是你透明度没有了,还占着位置,所以我们需要知道动画如何结束。
所以我们可以添加一个动画的监听:
- public void fadeOut(View view)
- {
- ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha", 0.5f);
- anim.addListener(new AnimatorListener()
- {
- @Override
- public void onAnimationStart(Animator animation)
- {
- Log.e(TAG, "onAnimationStart");
- }
- @Override
- public void onAnimationRepeat(Animator animation)
- {
- // TODO Auto-generated method stub
- Log.e(TAG, "onAnimationRepeat");
- }
- @Override
- public void onAnimationEnd(Animator animation)
- {
- Log.e(TAG, "onAnimationEnd");
- ViewGroup parent = (ViewGroup) mBlueBall.getParent();
- if (parent != null)
- parent.removeView(mBlueBall);
- }
- @Override
- public void onAnimationCancel(Animator animation)
- {
- // TODO Auto-generated method stub
- Log.e(TAG, "onAnimationCancel");
- }
- });
- anim.start();
- }
这样就可以监听动画的开始、结束、被取消、重复等事件~但是有时候会觉得,我只要知道结束就行了,这么长的代码我不能接收,那你可以使用AnimatorListenerAdapter
- anim.addListener(new AnimatorListenerAdapter()
- {
- @Override
- public void onAnimationEnd(Animator animation)
- {
- Log.e(TAG, "onAnimationEnd");
- ViewGroup parent = (ViewGroup) mBlueBall.getParent();
- if (parent != null)
- parent.removeView(mBlueBall);
- }
- });
6、AnimationSet使用
- public void togetherRun(View view)
- {
- ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",
- 1.0f, 2f);
- ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",
- 1.0f, 2f);
- AnimatorSet animSet = new AnimatorSet();
- animSet.setDuration(2000);
- animSet.setInterpolator(new LinearInterpolator());
- //两个动画同时执行
- animSet.playTogether(anim1, anim2);
- animSet.start();
- }
- public void playWithAfter(View view)
- {
- float cx = mBlueBall.getX();
- ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",
- 1.0f, 2f);
- ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",
- 1.0f, 2f);
- ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall,
- "x", cx , 0f);
- ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall,
- "x", cx);
- /**
- * anim1,anim2,anim3同时执行
- * anim4接着执行
- */
- AnimatorSet animSet = new AnimatorSet();
- animSet.play(anim1).with(anim2);
- animSet.play(anim2).with(anim3);
- animSet.play(anim4).after(anim3);
- animSet.setDuration(1000);
- animSet.start();
- }