Android动画 帧动画、补间动画、属性动画 (一)

2024-04-17 06:48
文章标签 android 属性 动画 补间

本文主要是介绍Android动画 帧动画、补间动画、属性动画 (一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

android中动画有三种

帧动画(Frame Animation)、补间动画(Tween Animation)、属性动画(Property Animation)

一、帧动画(Frame Animation)

具体实现过程:

1、在res/drawable目录下一个文件progress_animlist.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<!--根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画  根标签下,通过item标签对动画中的每一个图片进行声明  android:duration 表示展示所用的该图片的时间长度  -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false" ><item
        android:drawable="@drawable/p1"android:duration="150"></item><item
        android:drawable="@drawable/p2"android:duration="150"></item><item
        android:drawable="@drawable/p3"android:duration="150"></item><item
        android:drawable="@drawable/p4"android:duration="150"></item><item
        android:drawable="@drawable/p5"android:duration="150"></item><item
        android:drawable="@drawable/p6"android:duration="150"></item><item
        android:drawable="@drawable/p7"android:duration="150"></item><item
        android:drawable="@drawable/p8"android:duration="150"></item>
</animation-list>

根节点是animation-list(动画列表),里面有一个或者多个item节点组成,oneshot属性表示是否只播放一次,true表示只会播放一次,false表示一直循环播放,内部用item节点声明一个动画帧,android:drawable指定此帧动画所对应的图片资源,android:druation代表此帧持续的时间,整数,单位为毫秒。

2、用ImageView控件作为动画载体来显示动画
  <ImageView
        android:src="@drawable/progress_animlist"android:id="@+id/animi"android:layout_width="match_parent"android:layout_height="match_parent"/>

纯java代码方式

//创建帧动画
AnimationDrawable animationDrawable = new AnimationDrawable();
//添加帧       animationDrawable.addFrame(getResources().getDrawable(R.mipmap.p1), 300);        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.p2), 300);        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.p3), 300);        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.p4), 300);       animationDrawable.addFrame(getResources().getDrawable(R.mipmap.p5), 300);//设置动画是否只播放一次, 默认是false
animationDrawable.setOneShot(false);//根据索引获取到那一帧的时长
int duration = animationDrawable.getDuration(2);//根据索引获取到那一帧的图片
Drawable drawable = animationDrawable.getFrame(0);//判断是否是在播放动画
boolean isRunning = animationDrawable.isRunning();//获取这个动画是否只播放一次
boolean isOneShot = animationDrawable.isOneShot();//获取到这个动画一共播放多少帧
int framesCount = animationDrawable.getNumberOfFrames();//把这个动画设置为background,兼容更多版本写下面那句
mIvImg.setBackground(animationDrawable);
mIvImg.setBackgroundDrawable(animationDrawable);//开始播放动画
animationDrawable.start();
//停止播放动画
animationDrawable.stop();

二、补间动画(Tween Animation)

补间动画可以用java代码实现,也可以通过xml文件进行配置

动画类型Java代码实现方式XML配置方式
渐变透明度动画效果AlphaAnimationalpha
缩放动画效果ScaleAnimationscale
旋转动画效果RotateAnimationrotate
位置移动动画效果TranslateAnimationtranslate
组合动画效果AnimationSetset

具体实现

1、alpha渐变透明度动画效果

(1)、xml方式
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"android:duration="500"android:fillAfter="false"android:fromAlpha="1.0"android:toAlpha="0.5" /><!--透明度控制动画效果 alpha浮点型值:fromAlpha 属性为动画起始时透明度toAlpha   属性为动画结束时透明度说明:0.0表示完全透明1.0表示完全不透明以上值取0.0-1.0之间的float数据类型的数字长整型值:duration  属性为动画持续时间说明:时间以毫秒为单位-->

XML方式加载方式通过AnimationUtils.loadAnimation(this, R.anim.anim_alpha)获取Animation

Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
imageView.startAnimation(alphaAnimation);
(2)、Java代码方式
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(500);//设置动画持续时间为500毫秒
alphaAnimation.setFillAfter(false);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)
imageView.startAnimation(alphaAnimation);

2、scale缩放动画效果

(1)、xml方式
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"android:duration="500"android:fromXScale="0.0"android:fromYScale="0.0"android:interpolator="@android:anim/decelerate_interpolator"android:pivotX="50%"android:pivotY="50%"android:repeatCount="1"android:repeatMode="reverse"android:startOffset="0"android:toXScale="1.5"android:toYScale="1.5" /><!--尺寸伸缩动画效果 scale属性:interpolator 指定一个动画的插入器在我试验过程中,使用android.res.anim中的资源时候发现有三种动画插入器:accelerate_decelerate_interpolator  加速-减速 动画插入器accelerate_interpolator        加速-动画插入器decelerate_interpolator        减速- 动画插入器其他的属于特定的动画效果repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变浮点型值:fromXScale 属性为动画起始时 X坐标上的伸缩尺寸    toXScale   属性为动画结束时 X坐标上的伸缩尺寸     fromYScale 属性为动画起始时Y坐标上的伸缩尺寸    toYScale   属性为动画结束时Y坐标上的伸缩尺寸    说明:以上四种属性值    0.0表示收缩到没有 1.0表示正常无伸缩     值小于1.0表示收缩  值大于1.0表示放大pivotX     属性为动画相对于物件的X坐标的开始位置pivotY     属性为动画相对于物件的Y坐标的开始位置说明:以上两个属性值 从0%-100%中取值50%为物件的X或Y方向坐标上的中点位置长整型值:duration  属性为动画持续时间说明:   时间以毫秒为单位startOffset 动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒repeatCount,动画重复的计数,动画将会执行该值+1次布尔型值:fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用-->

XML方式加载方式通过AnimationUtils.loadAnimation(this, R.anim.anim_scale)获取Animation

Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
imageView.startAnimation(alphaAnimation);
(2)、Java代码方式
 Animation scaleAnimation = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);scaleAnimation.setDuration(500);//设置动画持续时间为500毫秒scaleAnimation.setFillAfter(true);//如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态scaleAnimation.setFillBefore(false);//如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态scaleAnimation.setRepeatCount(3);//设置动画循环次数scaleAnimation.setRepeatMode(Animation.REVERSE);scaleAnimation.setStartOffset(0);scaleAnimation.setInterpolator(this, android.R.anim.decelerate_interpolator);//设置动画插入器imageView.startAnimation(scaleAnimation);

3、rotate旋转动画效果

(1)、xml方式
<rotate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="500"android:fromDegrees="0"android:interpolator="@android:anim/accelerate_decelerate_interpolator"android:pivotX="50%"android:pivotY="50%"android:toDegrees="-360" />
<!--rotate 旋转动画效果属性:interpolator 指定一个动画的插入器在我试验过程中,使用android.res.anim中的资源时候发现有三种动画插入器:accelerate_decelerate_interpolator   加速-减速 动画插入器accelerate_interpolator               加速-动画插入器decelerate_interpolator               减速- 动画插入器其他的属于特定的动画效果浮点数型值:fromDegrees 属性为动画起始时物件的角度    toDegrees   属性为动画结束时物件旋转的角度 可以大于360度   说明:当角度为负数——表示逆时针旋转当角度为正数——表示顺时针旋转              (负数from——to正数:顺时针旋转)   (负数from——to负数:逆时针旋转) (正数from——to正数:顺时针旋转) (正数from——to负数:逆时针旋转)       pivotX     属性为动画相对于物件的X坐标的开始位置pivotY     属性为动画相对于物件的Y坐标的开始位置说明:        以上两个属性值 从0%-100%中取值50%为物件的X或Y方向坐标上的中点位置长整型值:duration  属性为动画持续时间说明:       时间以毫秒为单位-->

XML方式加载方式通过AnimationUtils.loadAnimation(this, R.anim.anim_rotate)获取Animation

Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
imageView.startAnimation(alphaAnimation);
(2)、Java代码方式
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(500);
rotateAnimation.setFillAfter(true);
rotateAnimation.setInterpolator(this, android.R.anim.accelerate_decelerate_interpolator);//设置动画插入器
imageView.startAnimation(rotateAnimation);

4、translate位置移动动画效果

(1)、xml方式
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="500"android:fromXDelta="100"android:fromYDelta="0"android:interpolator="@android:anim/cycle_interpolator"android:toXDelta="0"android:toYDelta="0" /><!--translate 位置转移动画效果整型值:fromXDelta 动画起始时 X坐标上的位置    toXDelta   动画结束时 X坐标上的位置fromYDelta 动画起始时 Y坐标上的位置toYDelta   动画结束时 Y坐标上的位置注意:没有指定fromXType toXType fromYType toYType 时候,默认是以自己为相对参照物             长整型值:duration  属性为动画持续时间说明:   时间以毫秒为单位-->

XML方式加载方式通过AnimationUtils.loadAnimation(this, R.anim.anim_translate)获取Animation

Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
imageView.startAnimation(alphaAnimation);
(2)、Java代码方式
Animation translateAnimation = new TranslateAnimation(0, 100, 0, 0);
translateAnimation.setDuration(500);
translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);//设置动画插入器
translateAnimation.setFillAfter(true);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)
imageView.startAnimation(translateAnimation);

5、set组合动画效果

(1)、xml方式
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><alpha
        android:duration="500"android:fromAlpha="1.0"android:toAlpha="0.0" /><scale
        android:duration="500"android:fromXScale="0.0"android:fromYScale="0.0"android:interpolator="@android:anim/decelerate_interpolator"android:pivotX="50%"android:pivotY="50%"android:repeatCount="1"android:repeatMode="reverse"android:startOffset="0"android:toXScale="1.5"android:toYScale="1.5" />
</set>

XML方式加载方式通过AnimationUtils.loadAnimation(this, R.anim.anim_set)获取Animation

AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_set);
imageView.startAnimation(animationSet);
(2)、Java代码方式
AnimationSet animationSet = new AnimationSet(true);Animation alphaAnimation = new AlphaAnimation(1.0f, 0.1f);
alphaAnimation.setDuration(500);//设置动画持续时间为500毫秒Animation scaleAnimation = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(500);//设置动画持续时间为500毫秒
scaleAnimation.setRepeatMode(Animation.REVERSE);
scaleAnimation.setStartOffset(0);
scaleAnimation.setInterpolator(this, android.R.anim.decelerate_interpolator);//设置动画插入器animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);imageView.startAnimation(animationSet);

这篇关于Android动画 帧动画、补间动画、属性动画 (一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/911080

相关文章

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

MySQL 事务的概念及ACID属性和使用详解

《MySQL事务的概念及ACID属性和使用详解》MySQL通过多线程实现存储工作,因此在并发访问场景中,事务确保了数据操作的一致性和可靠性,下面通过本文给大家介绍MySQL事务的概念及ACID属性和... 目录一、什么是事务二、事务的属性及使用2.1 事务的 ACID 属性2.2 为什么存在事务2.3 事务

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

Spring Boot 事务详解(事务传播行为、事务属性)

《SpringBoot事务详解(事务传播行为、事务属性)》SpringBoot提供了强大的事务管理功能,通过@Transactional注解可以方便地配置事务的传播行为和属性,本文将详细介绍Spr... 目录Spring Boot 事务详解引言声明式事务管理示例编程式事务管理示例事务传播行为1. REQUI

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后