Property Animation学习

本文主要源码参考的是Hyman的博客http://blog.csdn.net/lmj623565791/article/details/38067475,支持原创,再次感谢hyman给予的帮助

1.概况

  1. 3.0之后引入,动画本质是的过程是反射出属性值,接着改变它,然后刷新界面
  2. 区别View Animation(改变的只是绘制效果,真正的View属性是没有变化的,比如按键大小...)
  3. 区别Drawable Animation(帧动画)
  4. 参考Hyman的博客:http://blog.csdn.net/lmj623565791/article/details/38067475

继承结构是:
Animation
    |
ValueAnimation
    |
ObjectAnimation

其中
  1. Animation中申明了一些运行动画和停止动画,还有AnimatorListener(接收animaton运行时的运动事件(onStart,onEnd,onCancle,onRepeat)的回调接口)
  2. ValueAnimation 提供一个计时器计算动画值,还提供AnimatorUpdateListtener--提供动画属性值改变时的回调接口,和ObjectAnimation唯一的区别是不提供作用对象(targetObject)
           提供一个 setEvaluator (TypeEvaluator value)方法设置计时器,
  1. ObjectAnimation 提供改变对象属性的动画

2.通过ValueAnimation实现动画
好处:不需要操作的对象的属性一定要有getter和setter方法,你可以自己根据当前动画的计算值,来操作任何属性

  1. public void verticalRun(View view)  
  2.     {  
  3.         ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight  
  4.                 - mBlueBall.getHeight());  
  5.         animator.setTarget(mBlueBall);  
  6.         animator.setDuration(1000).start();  
  7.     } 

由于是没有设置需要运动的对象,需要设置addUpdateListener监听,如下:

  1. /** 
  2.      * 自由落体 
  3.      * @param view 
  4.      */  
  5.     public void verticalRun( View view)  
  6.     {  
  7.         ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight  
  8.                 - mBlueBall.getHeight());  
  9.         animator.setTarget(mBlueBall);  
  10.         animator.setDuration(1000).start();  
  11. //      animator.setInterpolator(value)  
  12.         animator.addUpdateListener(new AnimatorUpdateListener()  
  13.         {  
  14.             @Override  
  15.             public void onAnimationUpdate(ValueAnimator animation)  
  16.             {  
  17.                 mBlueBall.setTranslationY((Float) animation.getAnimatedValue());  
  18.             }  
  19.         });  
  20.     }  

抛物线:

  1. /** 
  2.      * 抛物线 
  3.      * @param view 
  4.      */  
  5.     public void paowuxian(View view)  
  6.     {  
  7.   
  8.         ValueAnimator valueAnimator = new ValueAnimator();  
  9.         valueAnimator.setDuration(3000);  
  10.         valueAnimator.setObjectValues(new PointF(00));  
  11.         valueAnimator.setInterpolator(new LinearInterpolator());  
  12.         valueAnimator.setEvaluator(new TypeEvaluator<PointF>()  
  13.         {  
  14.             // fraction = t / duration  
  15.             @Override  
  16.             public PointF evaluate(float fraction, PointF startValue,  
  17.                     PointF endValue)  
  18.             {  
  19.                 Log.e(TAG, fraction * 3 + "");  
  20.                 // x方向200px/s ,则y方向0.5 * 10 * t  
  21.                 PointF point = new PointF();  
  22.                 point.x = 200 * fraction * 3;  
  23.                 point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);  
  24.                 return point;  
  25.             }  
  26.         });  
  27.   
  28.         valueAnimator.start();  
  29.         valueAnimator.addUpdateListener(new AnimatorUpdateListener()  
  30.         {  
  31.             @Override  
  32.             public void onAnimationUpdate(ValueAnimator animation)  
  33.             {  
  34.                 PointF point = (PointF) animation.getAnimatedValue();  
  35.                 mBlueBall.setX(point.x);  
  36.                 mBlueBall.setY(point.y);  
  37.   
  38.             }  
  39.         });  

3.ObjectAnimation实现动画
比较简单

  1. package com.example.zhy_property_animation;  
  2.   
  3. import android.animation.ObjectAnimator;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7.   
  8. public class ObjectAnimActivity extends Activity 
  9. {  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState)  
  12.     {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.xml_for_anim);  
  15.     }  
  16.   
  17.     public void rotateyAnimRun(View view)  
  18.     {  
  19.          ObjectAnimator//  
  20.          .ofFloat(view, "rotationX"0.0F, 360.0F)//  
  21.          .setDuration(500)//  
  22.          .start();  
  23.     }  
  24.   
  25. }

4.PropertyValuesHolder一个动画实现多个效果

  1. public void propertyValuesHolder(View view)  
  2.     {  
  3.         PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,  
  4.                 0f, 1f);  
  5.         PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,  
  6.                 0, 1f);  
  7.         PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,  
  8.                 0, 1f);  
  9.         ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();  
  10.     }

5、监听动画的事件

对于动画,一般都是一些辅助效果,比如我要删除个元素,我可能希望是个淡出的效果,但是最终还是要删掉,并不是你透明度没有了,还占着位置,所以我们需要知道动画如何结束。

所以我们可以添加一个动画的监听:

  1. public void fadeOut(View view)  
  2.     {  
  3.         ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha"0.5f);  
  4.           
  5.         anim.addListener(new AnimatorListener()  
  6.         {  
  7.   
  8.             @Override  
  9.             public void onAnimationStart(Animator animation)  
  10.             {  
  11.                 Log.e(TAG, "onAnimationStart");  
  12.             }  
  13.   
  14.             @Override  
  15.             public void onAnimationRepeat(Animator animation)  
  16.             {  
  17.                 // TODO Auto-generated method stub  
  18.                 Log.e(TAG, "onAnimationRepeat");  
  19.             }  
  20.   
  21.             @Override  
  22.             public void onAnimationEnd(Animator animation)  
  23.             {  
  24.                 Log.e(TAG, "onAnimationEnd");  
  25.                 ViewGroup parent = (ViewGroup) mBlueBall.getParent();  
  26.                 if (parent != null)  
  27.                     parent.removeView(mBlueBall);  
  28.             }  
  29.   
  30.             @Override  
  31.             public void onAnimationCancel(Animator animation)  
  32.             {  
  33.                 // TODO Auto-generated method stub  
  34.                 Log.e(TAG, "onAnimationCancel");  
  35.             }  
  36.         });  
  37.         anim.start();  
  38.     }  

这样就可以监听动画的开始、结束、被取消、重复等事件~但是有时候会觉得,我只要知道结束就行了,这么长的代码我不能接收,那你可以使用AnimatorListenerAdapter

  1. anim.addListener(new AnimatorListenerAdapter()  
  2. {  
  3.     @Override  
  4.     public void onAnimationEnd(Animator animation)  
  5.     {  
  6.         Log.e(TAG, "onAnimationEnd");  
  7.         ViewGroup parent = (ViewGroup) mBlueBall.getParent();  
  8.         if (parent != null)  
  9.             parent.removeView(mBlueBall);  
  10.     }  
  11. }); 

6、AnimationSet使用

  1.  public void togetherRun(View view)  
  2.     {  
  3.         ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",  
  4.                 1.0f, 2f);  
  5.         ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",  
  6.                 1.0f, 2f);  
  7.         AnimatorSet animSet = new AnimatorSet();  
  8.         animSet.setDuration(2000);  
  9.         animSet.setInterpolator(new LinearInterpolator());  
  10.         //两个动画同时执行  
  11.         animSet.playTogether(anim1, anim2);  
  12.         animSet.start();  
  13.     }  
  14.   
  15.     public void playWithAfter(View view)  
  16.     {  
  17.         float cx = mBlueBall.getX();  
  18.   
  19.         ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",  
  20.                 1.0f, 2f);  
  21.         ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",  
  22.                 1.0f, 2f);  
  23.         ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall,  
  24.                 "x",  cx ,  0f);  
  25.         ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall,  
  26.                 "x", cx);  
  27.           
  28.         /** 
  29.          * anim1,anim2,anim3同时执行 
  30.          * anim4接着执行 
  31.          */  
  32.         AnimatorSet animSet = new AnimatorSet();  
  33.         animSet.play(anim1).with(anim2);  
  34.         animSet.play(anim2).with(anim3);  
  35.         animSet.play(anim4).after(anim3);  
  36.         animSet.setDuration(1000);  
  37.         animSet.start();  
  38.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值