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

相关文章

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Java 正则表达式的使用实战案例

《Java正则表达式的使用实战案例》本文详细介绍了Java正则表达式的使用方法,涵盖语法细节、核心类方法、高级特性及实战案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、正则表达式语法详解1. 基础字符匹配2. 字符类([]定义)3. 量词(控制匹配次数)4. 边

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C