Android animations的使用

2023-10-08 00:10
文章标签 android 使用 animations

本文主要是介绍Android animations的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

animations的使用


       Animations

           一 . Animations简介

                   Animations是谷歌官方提供的在安卓UI上进行一系列动画操作的API,可以对大多数的控件进行旋转(rotate) , 移动(translation) , 淡入淡出(alpha) , 以及放缩(scale)等操作,方便我们做出更加友好的UI界面.

            二 . Animations的分类

                  Animations从总体上可以分为两大类:
                  1.Tweened Animations:  该类Animations提供了旋转 , 移动 , 伸展 和淡入淡出的效. 
                  2.Frame-by_frame Animations:  这一类Animations可以创建一个Drawable序列,这写Drawable可以按照指定的时间一个一个的显示出来.

三 . Animations的使用方法

使用TweenedAnimations的步骤:
1 . 创建一个AnimationSet对象(Animations的子类),这个可以看作是Animations的集合,可以放入很多个不同的动画到一个AnimationSet对象中,以此来合成几个动画.
2 . 创建相应的Animations对象,设置好各种参数.
3 . 将创建并设置好参数的Animations对象挨个的添加入AnimationSet的对象中去.
4 . 对需要执行动画的控件调用startAnimation()函数,来执行相应的动画(当然这里不一定需要AnimationSet,也可以直接让控件执行单个的Animations)
上面在介绍的是后已经说过了,TweenedAnimnation有四种效果,那么我们应该可以猜测到,Animation的四个子类f分别是:AlphaAnimation , TranslateAnimation , ScaleAnimation , RotateAnimation

四. Animations实战

上面介绍完后,马上进入实战的环节.在安卓上使用Animations一般有两种方式:

第一种就是纯java的代码来定义需要的animations 

第二种就是用.xml文件来定义需要的animations , 只需要用java的代码来和控件绑定执行就可以了,后面一种降低了耦合,同时也增加了调试时的复杂程度.


我们先做一个小的布局,将四种按钮以及后面的混合动画按钮都放上去,而我们的操作对象就是一个放了张图片的ImageView

activity_main.xml

<pre name="code" class="html" style="font-size: 14px; font-family: Arial, Helvetica, sans-serif;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/button_rotate"android:text="旋转"android:layout_width="70dp"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/button_translation"android:text="移动"android:layout_width="70dp"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_toEndOf="@+id/button_rotate" /><Buttonandroid:id="@+id/button_alpha"android:text="淡入淡出"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_toEndOf="@+id/button_translation" /><Buttonandroid:id="@+id/button_scale"android:text="放缩"android:layout_width="70dp"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_toStartOf="@+id/button_all" /><Buttonandroid:id="@+id/button_all"android:text="混合"android:layout_width="70dp"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_alignParentEnd="true" /><ImageViewandroid:id="@+id/imageview"android:background="@drawable/ic_launcher"android:layout_width="300dp"android:layout_height="300dp"android:layout_centerVertical="true"android:layout_centerHorizontal="true" /></RelativeLayout>
</pre><p></p><h3 style="font-size:14px; font-family:Arial,Helvetica,sans-serif"><span style="color:#9999ff">           第一种方法,纯java代码运用Animation:</span></h3><p style="font-size:14px; font-family:Arial,Helvetica,sans-serif"><span style="font-family:Arial,Helvetica,sans-serif">           下面我们就开始定制动画了</span></p><p style="font-size:14px; font-family:Arial,Helvetica,sans-serif"></p><pre name="code" class="html">/*** 淡入淡出的动画** 参数1:起始的透明度,1为不透明* 参数2:终止的透明度,0为全透明*/alphaAnimation = new AlphaAnimation(1,0);/*** 该动画执行的时间长度*/alphaAnimation.setDuration(500);



/*** 旋转的动画** 参数1:从哪个旋转角度开始* 参数2:转到什么角度* 参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、*       RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标* 参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴* 参数5:确定y轴坐标的类型* 参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴*/rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);rotateAnimation.setDuration(1000);

/*** 放缩的动画** 参数1:x轴的初始值* 参数2:x轴收缩后的值* 参数3:y轴的初始值* 参数4:y轴收缩后的值* 参数5:确定x轴坐标的类型* 参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴* 参数7:确定y轴坐标的类型* 参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴*/scaleAnimation = new ScaleAnimation(0,0.1f,0,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);scaleAnimation.setDuration(1000);


/*** 移动的动画** 参数1:相对与父控件还是自身(与旋转的参数意思相同)* 参数2:确定X轴开始的位置* 参数3:类似参数1* 参数4:y轴开始的位置* 参数5,6:确定x轴终止位置* 参数7,8:确定Y轴终止位置*/translateAnimation =new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f);translateAnimation.setDuration(1000);


上面的代码分别定义了四种动画:在按钮的监听器中只需要加入一行代码,

                        imageView.startAnimation(rotateAnimation);
即可开始执行相应的动画效果了..

 

第二种方法,用xml文件定义Animation:

第二种方法用到了.xml文件来定义动画:

1. 首先在res文件夹下建立一个anim的文件夹,专门用来存放定义的动画

2. 在这个文件夹里定义Animation Resource File.如下图所示


1.alpha_animation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"><alphaandroid:fromAlpha="1.0"android:toAlpha="0.0"android:startOffset="500"android:duration="500"/>
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"><!--fromDegrees:开始的角度toDegrees:结束的角度,+表示是正的pivotX:用于设置旋转时的x轴坐标例1)当值为"50",表示使用绝对位置定位2)当值为"50%",表示使用相对于控件本身定位3)当值为"50%p",表示使用相对于控件的父控件定位pivotY:用于设置旋转时的y轴坐标--><rotateandroid:fromDegrees="0"android:toDegrees="+360"android:pivotX="50%"android:pivotY="50%"android:duration="1000"/>
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"><!--起始x轴坐标止x轴坐标始y轴坐标止y轴坐标轴的坐标轴的坐标--><scaleandroid:fromXScale="1.0"android:toXScale="0.0"android:fromYScale="1.0"android:toYScale="0.0"android:pivotX="50%"android:pivotY="50%"android:duration="1000"/>
</set>


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"><!--始x轴坐标止x轴坐标始y轴坐标止y轴坐标--><translateandroid:fromXDelta="0%"android:toXDelta="100%"android:fromYDelta="0%"android:toYDelta="100%"android:duration="2000"/>
</set>
</pre><pre name="code" class="html">上面在四个xml文件里面定义好了不同的动画效果,接下来需要使用它们:          
                  Animation animation = AnimationUtils.loadAnimation( Animation1Activity.this, R.anim.alpha);       //先将xml文件里面的动画效果装载出
<span style="font-family: Arial, Helvetica, sans-serif;">                 imageView.startAnimation(Animation);          //和上面用法一样的一步,将动画效果作用于控件</span>

 AnimationSet的用法

                 说完了如何使用Animations之后,开始讲讲最后一个AnimationSet的用法,我们可以将任意的动画放入一个Animations中:
<span style="font-size:14px;font-weight: normal;">/*** 创建了一个AnimastionSet对象* 在点击buttonAll的时候我们使用它* 其他四个按钮点击的时候我们直接使用创建好了的Animations** 里面有一个boolean类型的参数* 如果设置为true,则里面存放的所有animations都使用自己的interpolator(执行动画的方式,如先加速后减速等)* 如果设置为false,则使用AnimationSet统一的interpolator*/</span>
<span style="font-size:14px;font-weight: normal;">        animationSet = new AnimationSet(true);</span>
</pre><pre name="code" class="java"><span style="font-size:14px;font-weight: normal;">        接着我们将上面的四个动画挑选几个放入AniamtionSet中,小达这里将旋转和移动放入其中,也就是:</span>
<span style="font-size:14px;font-weight: normal;">                animationSet.addAnimation(rotateAnimation);</span>
<span style="font-size:14px;font-weight: normal;">                animationSet.addAnimation(translateAnimation);</span>
</pre><pre name="code" class="java">      <span style="font-weight: normal;"><span style="font-size:14px;"> 再给按钮加上点击事件,就可以执行动画了~~~~~~还有什么不明白的地方可以给我留言噢,还有直接Q我也行,2319821734,有什么好的东西我会及时拿上来和大家分享的.</span></span>

 
 
 

 

 

 

这篇关于Android animations的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定