Android实现动画效果详解
作者:掌缘生灭 时间:2022-04-19 20:37:43
目前Android平台提供了两类动画一类是Tween动画,第二类就是 Frame动画,具体内容介绍请看下文:
一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。
第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。
实现动画有两种方式:一种使用XML文件(文件放在res/anim),一种直接代码搞定
1、透明度控制动画效果alpha
<!--
透明度控制动画效果alpha
浮点型值:
fromAlpha 动画起始时透明度
toAlpha 动画结束时透明度
说明:0.0 完全透明
1.0 完全不透明
以上值取0.0-1.0之间的 float数据类型的数字
duration 为动画持续时间
长整型:
说明:时间以毫秒为单位
-->
<alpha
android:duration="3000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
代码方式:
Animation animationAlpha = new AlphaAnimation(0.0f, 1.0f);
animationAlpha.setDuration(3000);
ivAnim.startAnimation(animationAlpha);
2、rotate旋转动画
<!--
rotate旋转动画效果
属性:interpolator 指定一个动画的插入器
有三种动画插入器:
accelerate_decelerate_interpolator 加速-减速 动画插入器
accelerate_interpolator 加速-动画插入器
decelerate_interpolator 减速-动画插入器
其他的属于特定的动画效果
浮点整型值:
fromDegrees 为动画起始时物件的角度
toDegrees 为动画起始时物件旋转的角度 可以大于360度
说明:当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数from——to正数:顺时针旋转
负数from——to负数:逆时针旋转
正数from——to正数:顺时针旋转)
pivotX 为动画相对于物件的X坐标的开始位置
pivotY 为动画相对于物件的Y坐标的开始位置
说明:以上两个属性值 从0%——100%中取值
50%为物件的X或Y方向坐标上的中点位置
长整型类型:
duration 为动画持续时间
说明:时间以毫秒为单位
-->
<rotate
android:duration="3000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+350" />
Animation animationRotate = new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationRotate.setDuration(3000);
ivAnim.startAnimation(animationRotate);
3、尺寸伸缩动画效果 scale
<!--
尺寸伸缩动画效果 scale
属性:interpolator 指定一个动画的插入器
有三种动画插入器:
accelerate_decelerate_interpolator 加速-减速 动画插入器
accelerate_interpolator 加速-动画插入器
decelerate_interpolator 减速-动画插入器
其他的属于特定的动画效果
浮点型值:
fromXScale 动画起始时 X坐标上的伸缩尺寸
toXScale 动画结束时 X坐标上的伸缩尺寸
fromYScale 动画起始 Y时坐标上的伸缩尺寸
toYScale 动画结束时 Y坐标上的尺寸
说明:以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
pivotX 动画相对于物件的X坐标的开始位置
pivotY 动画相对于武将的Y坐标的开始位置
说明:以上两个属性值 从0%-100%中取值
长整型:
duration 动画持续时间
说明:时间以毫秒为单位
布尔型值:
fillAfter 当设置为true,该动画转化在动画结束后被应用
-->
<scale
android:duration="700"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.4"
android:toYScale="1.4" />
代码方式:
Animation animationScale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationScale.setDuration(3000);
ivAnim.startAnimation(animationScale);
4、translate 位置转移动画效果
<!--
translate 位置转移动画效果
整型值:
fromXDelta 动画起始时 X坐标上的位置
toXDelta 动画结束时 X坐标上的位置
fromYDelta 动画起始时 Y坐标上的位置
toYDlta 动画结束时 Y坐标上的位置
注意:没有指定fromXTra toXType fromYType toYType 时候,默认是以自己为相对参照物
长整型:duration 为动画持续时间
时间以毫秒为单位
-->
<translate
android:duration="2000"
android:fromXDelta="30"
android:fromYDelta="30"
android:toXDelta="-80"
android:toYDelta="300" />
Animation animationTranslate = new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
animationTranslate.setDuration(3000);
ivAnim.startAnimation(animationTranslate);
xml文件方式使用的话就两行代码
Animation anim = AnimationUtils.loadAnimation(activity, R.anim.anim_xxx);
ivAnim.startAnimation(anim);
5、frame帧动画(文件放在res/drawable)
<!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:duration 表示展示所用的该图片的时间长度
-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="@drawable/icon_frame1"
android:duration="200">
</item>
<item
android:drawable="@drawable/icon_frame2"
android:duration="200">
</item>
<item
android:drawable="@drawable/icon_frame3"
android:duration="200">
</item>
<item
android:drawable="@drawable/icon_frame4"
android:duration="200">
</item>
<item
android:drawable="@drawable/icon_frame5"
android:duration="200">
</item>
<item
android:drawable="@drawable/icon_frame6"
android:duration="50">
</item>
</animation-list>
xml帧动画使用代码:
ivFrame = (ImageView) findViewById(R.id.iv_frame_image);
ivFrame.setImageResource(R.drawable.anim_frame);
animation = (AnimationDrawable) ivFrame.getDrawable();
animation.setOneShot(false);//循环
animation.start();
标签:android,android动画
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 获得View宽高的几种方式总结
2021-11-12 05:06:49
![](https://img.aspxhome.com/file/2023/6/101846_0s.png)
手把手带你实现一个萌芽版的Spring容器
2023-03-10 15:45:44
![](https://img.aspxhome.com/file/2023/9/94759_0s.png)
Android编程实现动态支持多语言的方法
2021-11-01 12:38:25
MyBatis-Plus联表查询(Mybatis-Plus-Join)的功能实现
2023-11-25 03:24:33
![](https://img.aspxhome.com/file/2023/3/60023_0s.png)
Springboot-Shiro基本使用详情介绍
2022-10-13 03:02:42
![](https://img.aspxhome.com/file/2023/2/75702_0s.png)
浅谈shiro的SecurityManager类结构
2022-08-25 14:13:32
![](https://img.aspxhome.com/file/2023/9/129969_0s.jpg)
spring 注解如何开启声明式事务
2023-04-09 23:07:42
Springcloud Eureka配置及集群代码实例
2021-09-19 05:54:52
新手小白看过来学JAVA必过IO流File字节流字符流
2022-09-22 07:42:02
![](https://img.aspxhome.com/file/2023/8/69908_0s.jpg)
IDEA中使用Typora编辑md文件的方法
2022-11-28 09:50:47
![](https://img.aspxhome.com/file/2023/5/132365_0s.png)
RocketMQ消息生产者是如何选择Broker示例详解
2023-11-10 21:45:49
Android实现流动的渐变色边框效果
2023-06-28 08:24:13
![](https://img.aspxhome.com/file/2023/9/117129_0s.png)
Android扫描和生成二维码
2022-09-11 22:57:16
C#基于HttpWebRequest实现发送HTTP请求的方法分析
2022-01-04 22:51:03
Java如何使用httpclient检测url状态及链接是否能打开
2022-07-03 21:23:25
string类的使用方法详解
2022-04-05 12:01:51
C语言char s[]和char* s的区别
2022-03-27 11:24:24
![](https://img.aspxhome.com/file/2023/5/114935_0s.png)
java的Arrays工具类实战
2023-08-21 15:46:36
![](https://img.aspxhome.com/file/2023/6/58076_0s.png)
详解如何在C#中使用投影(Projection)
2023-03-28 09:14:15
![](https://img.aspxhome.com/file/2023/1/119551_0s.png)
java eclipse 中文件的上传和下载示例解析
2021-11-18 06:55:58
![](https://img.aspxhome.com/file/2023/9/112049_0s.png)