上一节写的实现方式是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)
对应代码示例:
- <?xml version="1.0" encoding="utf-8"?>
- <scale xmlns:android="http://schemas.android.com/apk/res/android"
- android:fromXScale="0.0"
- android:toXScale="1.5"
- android:fromYScale="0.0"
- android:toYScale="1.5"
- android:pivotX="50"
- android:pivotY="50"
- android:duration="600" />
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
- scaleAnim = new ScaleAnimation(0.0f,1.5f,0.0f,1.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f;
- scaleAnim.setDuration(600);
(四)AlphaAnimation类的构造函数
- AlphaAnimation(Context context, AttributeSet attrs)
- AlphaAnimation(float fromAlpha, float toAlpha)
- 对应代码示例:
- <?xml version="1.0" encoding="utf-8"?>
- <alpha xmlns:android="http://schemas.android.com/apk/res/android"
- android:fromAlpha="1.0"
- android:toAlpha="0.2"
- android:duration="4000"
- android:fillBefore="true">
- </alpha>
↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓
- alphaAnim = new AlphaAnimation(1.0f,0.2f);
- alphaAnim.setDuration(4000);
- 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) 增加动画
- alphaAnim = new AlphaAnimation(1.0f,0.1f);
- scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
- rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- setAnim=new AnimationSet(true);
- setAnim.addAnimation(alphaAnim);
- setAnim.addAnimation(scaleAnim);
- setAnim.addAnimation(rotateAnim);
- setAnim.setDuration(4000);
- setAnim.setFillAfter(true);
(八)Interpolater
直接上代码通俗易懂
- ScaleAnimation interpolateScaleAnim=new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
- interpolateScaleAnim.setInterpolator(new BounceInterpolator());
- interpolateScaleAnim.setDuration(4000);