Android自定义动画学习,实现左右摇摆动画

本文介绍如何在Android中自定义动画,以实现特定的视觉效果。文章详细解释了通过继承Animation类并重写applyTransformation方法来创建自定义动画的过程,并提供了一个实现摇摆效果的具体示例。

(转载)http://johnnyg.iteye.com/blog/2074464

我们都知道Android SDK给我们提供了4种常用的动画效果分别是:

AlphaAnimation:透明度变化动画

RotateAnimation:旋转动画

ScaleAnimation: 缩放动画

TranslateAnimation:位置变化动画

但有时这些动画不能满足我们的要求,所以我们要通过自定义动画的手段来实现我们个性化的动画。

学习自定义动画很简单,我们可以参考上面这些SDK中动画类,看它们是如何实现的。就以RotateAnimation为例:

Java代码   收藏代码
  1. public class RotateAnimation extends Animation {  
  2.     private float mFromDegrees;  
  3.     private float mToDegrees;  
  4.   
  5.     private int mPivotXType = ABSOLUTE;  
  6.     private int mPivotYType = ABSOLUTE;  
  7.     private float mPivotXValue = 0.0f;  
  8.     private float mPivotYValue = 0.0f;  
  9.   
  10.     private float mPivotX;  
  11.     private float mPivotY;  
  12.   
  13.       
  14.     public RotateAnimation(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         // 省略 初始化变量那些事  
  17.     }  
  18.    
  19.     public RotateAnimation(float fromDegrees, float toDegrees) {  
  20.         // 省略 初始化变量那些事  
  21.     }  
  22.   
  23.     public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) {  
  24.         // 省略 初始化变量那些事  
  25.     }  
  26.   
  27.     public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,  
  28.             int pivotYType, float pivotYValue) {  
  29.        // 省略 初始化变量那些事  
  30.     }  
  31.   
  32.     private void initializePivotPoint() {  
  33.         if (mPivotXType == ABSOLUTE) {  
  34.             mPivotX = mPivotXValue;  
  35.         }  
  36.         if (mPivotYType == ABSOLUTE) {  
  37.             mPivotY = mPivotYValue;  
  38.         }  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  43.         float degrees = mFromDegrees + ((mToDegrees - mFromDegrees) * interpolatedTime);  
  44.         float scale = getScaleFactor();  
  45.           
  46.         if (mPivotX == 0.0f && mPivotY == 0.0f) {  
  47.             t.getMatrix().setRotate(degrees);  
  48.         } else {  
  49.             t.getMatrix().setRotate(degrees, mPivotX * scale, mPivotY * scale);  
  50.         }  
  51.     }  
  52.   
  53.     @Override  
  54.     public void initialize(int width, int height, int parentWidth, int parentHeight) {  
  55.         super.initialize(width, height, parentWidth, parentHeight);  
  56.         mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth);  
  57.         mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight);  
  58.     }  
  59. }  

 从该类代码来看,它无非就是重写了applyTransformation,和initialize两个方法而已,而从方法名和内容可以知道applyTransformation就是最核心的动画实现方法。我们在进一步看看这个方法在父类是怎么定义的,在父类Animation类中找到该方法的定义

Java代码   收藏代码
  1. /** 
  2.      * Helper for getTransformation. Subclasses should implement this to apply 
  3.      * their transforms given an interpolation value.  Implementations of this 
  4.      * method should always replace the specified Transformation or document 
  5.      * they are doing otherwise. 
  6.      *  
  7.      * @param interpolatedTime The value of the normalized time (0.0 to 1.0) 
  8.      *        after it has been run through the interpolation function. 
  9.      * @param t The Transformation object to fill in with the current 
  10.      *        transforms. 
  11.      */  
  12.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  13.     }  

 通过注释我们明白了(也可以结合调试理解):

在动画执行期间,这个方法会被不断回调

参数interpolatedTime:从方法被第一次回调时的0.0,随着动画的执行不断增长,当动画结束时这个值是1.0

参数t:我们通过变化它来实现动画的动作

 

我们可以再写一个例子玩一下这个方法

 

Java代码   收藏代码
  1. class CustomerAnimation extends Animation {  
  2.     @Override  
  3.     protected void applyTransformation(float interpolatedTime,  
  4.         Transformation t) {  
  5.         t.setAlpha(interpolatedTime);  
  6.     }  
  7. }  

 就这么几行代码,我们自己实现了个透明度变化动画。

 

 

现在我们该了解的都了解了,可以开始动手做“摇摆”动画了

Java代码   收藏代码
  1. class CustomerAnimation extends Animation {  
  2.     private int mWaveTimes=5;//摇摆次数  
  3.     private int mWaveRange=50;//摇摆幅度  
  4.               
  5.     public CustomerAnimation(){  
  6.                   
  7.     }  
  8.               
  9.     public CustomerAnimation(int waveTimes,int waveRange){  
  10.         mWaveTimes = waveTimes;  
  11.         mWaveRange = waveRange;  
  12.     }  
  13.               
  14.     @Override  
  15.     protected void applyTransformation(float interpolatedTime,  
  16.             Transformation t) {  
  17.         //运用周期性函数,实现左右摇摆  
  18.         t.getMatrix().setTranslate((int)(Math.sin(interpolatedTime*Math.PI*mWaveTimes)*mWaveRange),0);  
  19.     }  
  20. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值