天天看点

自定义控件(动画)第二节:Android的animation的四种类型的代码编写方式

上一节写的实现方式是xml格式,这种单一的方式肯定不能够满足我们今后的开发工作,毕竟需求一直在变嘛(呵呵),所以还涉及到代码编写的实现方式,也不复杂,基本上就是跟xml对应的关系书写方式。

(一)各标签对应的类:

scale ——  ScaleAnimation

alpha —— AlphaAnimation

rotate ——  RotateAnimation

translate  ——  TranslateAnimation

set  ——  AnimationSet

(二)animation基类的标签属性对应的方法:

 android:duration                  setDuration(long)  动画持续时间,以毫秒为单位 

android:fillAfter                    setFillAfter(boolean) 如果设置为true,控件动画结束时,将保持动画最后时的状态

android:fillBefore                 setFillBefore(boolean) 如果设置为true,控件动画结束时,还原到开始动画前的状态

android:fillEnabled              setFillEnabled(boolean) 与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态

android:repeatCount           setRepeatCount(int)  重复次数

android:repeatMode            setRepeatMode(int)  重复类型,有reverse和restart两个值,取值为RESTART或 REVERSE,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作。

android:interpolator            setInterpolator(Interpolator) 设定插值器,其实就是指定的动作效果,比如弹跳效果等

(三)ScaleAnimation类的构造函数

  • ScaleAnimation(Context context, AttributeSet attrs)  
  • ScaleAnimation(float fromX, float toX, float fromY, float toY)
  • ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
  • ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

对应代码示例:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromXScale="0.0"  
  4.     android:toXScale="1.5"  
  5.     android:fromYScale="0.0"  
  6.     android:toYScale="1.5"  
  7.     android:pivotX="50"  
  8.     android:pivotY="50"  
  9.     android:duration="600" />  

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

  1. scaleAnim = new ScaleAnimation(0.0f,1.5f,0.0f,1.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f;  
  2. scaleAnim.setDuration(600);  

(四)AlphaAnimation类的构造函数

  • AlphaAnimation(Context context, AttributeSet attrs)  
  • AlphaAnimation(float fromAlpha, float toAlpha)
  • 对应代码示例:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromAlpha="1.0"  
  4.     android:toAlpha="0.2"  
  5.     android:duration="4000"  
  6.     android:fillBefore="true">  
  7. </alpha>  

↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓

  1. alphaAnim = new AlphaAnimation(1.0f,0.2f);  
  2. alphaAnim.setDuration(4000);  
  3. alphaAnim.setFillBefore(true);  

(五)RotateAnimation类的构造函数

  • RotateAnimation(Context context, AttributeSet attrs)
  • RotateAnimation(float fromDegrees, float toDegrees)
  • RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
  • RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

(六)TranslateAnimation类的构造函数

  • TranslateAnimation(Context context, AttributeSet attrs)  
  • TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
  • TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

(七)AnimationSet类的构造函数

  • AnimationSet(Context context, AttributeSet attrs)  
  • AnimationSet(boolean shareInterpolator)  shareInterpolator取值true或false,取true时,指在AnimationSet中定义一个插值器(interpolater),它下面的所有动画共同。如果设为false,则表示它下面的动画自己定义各自的插值器。
  • public void addAnimation (Animation a) 增加动画
  1. alphaAnim = new AlphaAnimation(1.0f,0.1f);  
  2. scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
  3. rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
  4. setAnim=new AnimationSet(true);  
  5. setAnim.addAnimation(alphaAnim);  
  6. setAnim.addAnimation(scaleAnim);  
  7. setAnim.addAnimation(rotateAnim);  
  8. setAnim.setDuration(4000);  
  9. setAnim.setFillAfter(true);  

(八)Interpolater

直接上代码通俗易懂

  1. ScaleAnimation interpolateScaleAnim=new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
  2. interpolateScaleAnim.setInterpolator(new BounceInterpolator());  
  3. interpolateScaleAnim.setDuration(4000);  

继续阅读