前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android补间动画之ScaleAnimation、AlphaAnimation、RotateAnimation、TranslateAnimation、AnimationSet详解「建议收藏」

Android补间动画之ScaleAnimation、AlphaAnimation、RotateAnimation、TranslateAnimation、AnimationSet详解「建议收藏」

作者头像
全栈程序员站长
发布2022-11-07 15:32:15
2.1K0
发布2022-11-07 15:32:15
举报
文章被收录于专栏:全栈程序员必看

首发:http://blog.csdn.net/harvic880925/article/details/40117115

一、概述

前两篇,我为大家讲述了利用XML来定义动画及插值器,但在代码中,我们常常是动态生成动画的,所以,这篇将为大家讲述如何用代码生成动态生成动画及插值器。

先简单写出各个标签对应的类,方便大家理解:

  • scale —— ScaleAnimation
  • alpha —— AlphaAnimation
  • rotate —— RotateAnimation
  • translate —— TranslateAnimation
  • set —— AnimationSet

二、Animation公共类

官方SDK讲解页面为:《Animation》

第一篇中我们提到过,Animation类是所有动画(scale、alpha、translate、rotate)的基类,它所具有的标签及对应函数为:

  • 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) 设定插值器,其实就是指定的动作效果,比如弹跳效果等

在第一篇《 Animation 动画详解(一)——alpha、scale、translate、rotate、set的xml属性及用法》 我们已经讲解了每个标签具体所具有的功能,这里就不再细讲,对于使用方法会在下面的各标签中使用。

三、ScaleAnimation

这是scale标签对应的类,官方SDK页面为:《ScaleAnimation》

在Scale标签中,我们提到过它的自有属性有下面几条,先列一下:

  • android:fromXScale 起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;
  • android:toXScale 结尾的X方向上相对自身的缩放比例,浮点值;
  • android:fromYScale 起始的Y方向上相对自身的缩放比例,浮点值,
  • android:toYScale 结尾的Y方向上相对自身的缩放比例,浮点值;
  • android:pivotX 缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,当为数值时,表示在当前View的左上角,即原点处加上50px,做为起始缩放点;如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点;如果是50%p,那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标。(具体意义,后面会举例演示)
  • android:pivotY 缩放起点Y轴坐标,取值及意义跟android:pivotX一样。

放到代码中,ScaleAnimation有下面几个构造函数:

  • ScaleAnimation(Context context, AttributeSet attrs) 从XML文件加载动画,基本用不到
  • 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文件加载动画,基本用不到的,我们主要看下面三个构造函数。

在标签属性android:pivotX中有三种取值,数,百分数,百分数p;体现在构造函数中,就是最后一个构造函数的pivotXType,它的取值有三个,Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;

这三个构造函数难度不大,就不再细讲,举个例子说明:

在第一篇中Scale的例子的XML代码为:

[html] view plain copy

  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.4”
  5. android:fromYScale=“0.0”
  6. android:toYScale=“1.4”
  7. android:pivotX=“50”
  8. android:pivotY=“50”
  9. android:duration=“700” />

对应的代码构造代码为:

[java] view plain copy

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

在控件使用的时候,同样是使用:

[java] view plain copy

  1. tv.startAnimation(scaleAnim);

四、AlphaAnimation

这是alpha标签对就的类,官方SDK文档地址是: 《AlphaAnimation》 同样alpha标签自有的属性有:

  • android:fromAlpha 动画开始的透明度,从0.0 –1.0 ,0.0表示全透明,1.0表示完全不透明
  • android:toAlpha 动画结束时的透明度,也是从0.0 –1.0 ,0.0表示全透明,1.0表示完全不透明

所对应的构造函数为:

  • AlphaAnimation(Context context, AttributeSet attrs) 同样,从本地XML加载动画,基本不用
  • AlphaAnimation(float fromAlpha, float toAlpha)

这里只剩最后一个构造函数,难度不大,下面举个例子说明下用法。

在第一篇文章中,我们构造的XML代码为:

[java] view plain copy

  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.1”
  5. android:duration=“3000”
  6. android:fillBefore=“true”>
  7. </alpha>

如果用代码构造同样的效果,它所对应的代码为:

[java] view plain copy

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

五、RotateAnimation

RotateAnimation类对应Rotate标签,SDK文档地址:《RotateAnimation》

Rotate标签所具有的XML属性有:

  • android:fromDegrees 开始旋转的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
  • android:toDegrees 结束时旋转到的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
  • android:pivotX 缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,具体意义已在scale标签中讲述,这里就不再重讲
  • android:pivotY 缩放起点Y轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p

对应的构造函数有:

  • RotateAnimation(Context context, AttributeSet attrs)  从本地XML文档加载动画,同样,基本不用
  • 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)

RotateAnimation跟ScaleAnimation差不多,关键问题同样是pivotXType和pivotYType的选择,同样有三个取值:Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;

根据每一篇中的XML写出对应的JAVA构造代码:

XML为:

[java] view plain copy

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <rotate xmlns:android=“http://schemas.android.com/apk/res/android”
  3. android:fromDegrees=“0”
  4. android:toDegrees=“-650”
  5. android:pivotX=“50%”
  6. android:pivotY=“50%”
  7. android:duration=“3000”
  8. android:fillAfter=“true”>
  9. </rotate>

对应JAVA构造代码为:

[java] view plain copy

  1. rotateAnim = new RotateAnimation(0, –650, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  2. rotateAnim.setDuration(3000);
  3. rotateAnim.setFillAfter(true);

六、TranslateAnimation

很显示TranslateAnimation类对应translate标签,它的SDK官方文档地址为: 《TranslateAnimation》

translate标签所具有的属性为:

  • android:fromXDelta 起始点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,具体意义已在scale标签中讲述,这里就不再重讲
  • android:fromYDelta 起始点Y轴从标,可以是数值、百分数、百分数p 三种样式;
  • android:toXDelta 结束点X轴坐标
  • android:toYDelta 结束点Y轴坐标

这些属性所对应的构造函数为:

  • 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)

由于fromXDelta、fromYDelta、toXDelta、toYDelta这三个属性都具有三种状态,所以在构造函数中,最理想的状态就是第三个构造函数,能够指定每个值的类型,第二个构造函数:TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)使用是绝对数值。只有最后一个构造函数可以指定百分数和相对父控件的百分数。

下面以第一篇中的XML代码为例,用JAVA代码构造同样的效果:

XML代码:

[html] view plain copy

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <translate xmlns:android=“http://schemas.android.com/apk/res/android”
  3. android:fromXDelta=“0”
  4. android:toXDelta=“-80”
  5. android:fromYDelta=“0”
  6. android:toYDelta=“-80”
  7. android:duration=“2000”
  8. android:fillBefore=“true”>
  9. </translate>

对应的JAVA代码为:

[html] view plain copy

  1. translateAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80,
  2. Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80);
  3. translateAnim.setDuration(2000);
  4. translateAnim.setFillBefore(true);

七:AnimationSet

AnimationSet类对应set标签,定义动作类的集合,对应的SDK文档地址为: 《AnimationSet》 它自己是没有XML属性的,所以我们直接说它的构造函数:

  • AnimationSet(Context context, AttributeSet attrs) 同样,基本不用
  • AnimationSet(boolean shareInterpolator) shareInterpolator取值true或false,取true时,指在AnimationSet中定义一个插值器(interpolater),它下面的所有动画共同。如果设为false,则表示它下面的动画自己定义各自的插值器。

增加动画的函数为:(更多函数,请参看SDK文档)

  • public void addAnimation (Animation a)

下面在第一篇中的XML代码为例写出能构造同样效果的JAVA代码:

XML代码为:

[html] view plain copy

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <set xmlns:android=“http://schemas.android.com/apk/res/android”
  3. android:duration=“3000”
  4. android:fillAfter=“true”>
  5. <alpha
  6. android:fromAlpha=“0.0”
  7. android:toAlpha=“1.0”/>
  8. <scale
  9. android:fromXScale=“0.0”
  10. android:toXScale=“1.4”
  11. android:fromYScale=“0.0”
  12. android:toYScale=“1.4”
  13. android:pivotX=“50%”
  14. android:pivotY=“50%”/>
  15. <rotate
  16. android:fromDegrees=“0”
  17. android:toDegrees=“720”
  18. android:pivotX=“50%”
  19. android:pivotY=“50%”/>
  20. </set>

对应的JAVA代码为:

[java] view plain copy

  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(3000);
  9. setAnim.setFillAfter(true);

八、Interpolater插值器

关于插值器的效果及应用,我们专门开了一篇来讲,看这里: 《Animation动画详解(二)——Interpolator插值器》

关于插值器的SDK讲解见《Animation Resources》中的Interpolators部分;

插值器XML属性及对应的类如下表所示:

Interpolator class

Resource ID

AccelerateDecelerateInterpolator

@android:anim/accelerate_decelerate_interpolator

AccelerateInterpolator

@android:anim/accelerate_interpolator

AnticipateInterpolator

@android:anim/anticipate_interpolator

AnticipateOvershootInterpolator

@android:anim/anticipate_overshoot_interpolator

BounceInterpolator

@android:anim/bounce_interpolator

CycleInterpolator

@android:anim/cycle_interpolator

DecelerateInterpolator

@android:anim/decelerate_interpolator

LinearInterpolator

@android:anim/linear_interpolator

OvershootInterpolator

@android:anim/overshoot_interpolator

使用方法:(为sacleAnimation增加bounce插值器)

[java] view plain copy

  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(3000);

九、示例,源码

下面我把上面所有的代码集合到一个例子中,供大家下载;

效果图如下:

Android补间动画之ScaleAnimation、AlphaAnimation、RotateAnimation、TranslateAnimation、AnimationSet详解「建议收藏」
Android补间动画之ScaleAnimation、AlphaAnimation、RotateAnimation、TranslateAnimation、AnimationSet详解「建议收藏」
Android补间动画之ScaleAnimation、AlphaAnimation、RotateAnimation、TranslateAnimation、AnimationSet详解「建议收藏」
Android补间动画之ScaleAnimation、AlphaAnimation、RotateAnimation、TranslateAnimation、AnimationSet详解「建议收藏」

源码下载地址:http://download.csdn.net/detail/harvic880925/8047669

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183278.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、概述
  • 二、Animation公共类
  • 三、ScaleAnimation
  • 四、AlphaAnimation
  • 五、RotateAnimation
  • 六、TranslateAnimation
  • 七:AnimationSet
  • 八、Interpolater插值器
  • 九、示例,源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档