自定义控件(动画)第二节:Android的animation的四种类型的代码编写方式
2017-12-27 14:18
435 查看
上一节写的实现方式是xml格式,这种单一的方式肯定不能够满足我们今后的开发工作,毕竟需求一直在变嘛(呵呵),所以还涉及到代码编写的实现方式,也不复杂,基本上就是跟xml对应的关系书写方式。
scale
—— ScaleAnimation
alpha
—— AlphaAnimation
rotate
—— RotateAnimation
translate ——
TranslateAnimation
set
—— AnimationSet
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(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
4000
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(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(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(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(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);
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);
(一)各标签对应的类:
scale—— ScaleAnimation
alpha
—— AlphaAnimation
rotate
—— RotateAnimation
translate ——
TranslateAnimation
set
—— AnimationSet
(二)animation基类的标签属性对应的方法:
android:durationsetDuration(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
4000
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);
相关文章推荐
- 自定义控件(动画)第一节:Android的animation的四种类型和基本使用
- 21_Android中常见对话框,光传感器,通过重力感应器编写出指南针应用,帧动画,通过Jav代码的方式编写补间动画,通过XML的方式编写补间动画
- 动画类型 Android的animation由四种类型组成
- android 帧动画的四种启动方式
- Android自定义控件:进度条的四种实现方式(Progress Wheel的解析)(源码 + Demo)
- Android动画四种类型xml布局
- Android动画分类及编写方式(view Animation/Tween Animation)视图动画(一个对象的变形)
- Android的animation由四种类型组成
- Android animation的四种类型
- Android开发之自定义控件与属性动画Animation的结合使用
- Android自定义控件:进度条的四种实现方式(Progress Wheel的解析)(源码 + Demo)
- Android自定义控件:进度条的四种实现方式
- Android中编写属性动画PropertyAnimation的进阶实例
- listView扩展2——java代码方式实现animation动画输出
- ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码
- Android动画Animation之Tween用代码实现动画效果
- Android -- 样式和主题,用代码方式编写UI,用WebView通过html开发界面
- Android动画Animation的两种加载执行方式
- Android的animation的四种动画
- Android动画Animation的两种加载执行方式