Android--Animation动画介绍和实现
1.Animation 动画类型
Android的animation由四种类型组成:
XML中
alph | 渐变透明度动画效果 |
scale | 渐变尺寸伸缩动画效果 |
translate | 画面转换位置移动动画效果 |
rotate | 画面转移旋转动画效果 |
JavaCode中
AlphaAnimation | 渐变透明度动画效果 |
ScaleAnimation | 渐变尺寸伸缩动画效果 |
TranslateAnimation | 画面转换位置移动动画效果 |
RotateAnimation | 画面转移旋转动画效果 |
2.Android动画模式
Animation主要有两种动画模式:
一种是tweened animation(渐变动画)
XML中 | JavaCode |
alpha | AlphaAnimation |
scale | ScaleAnimation |
一种是frame by frame(画面转换动画)
XML中 | JavaCode |
translate | TranslateAnimation |
rotate | RotateAnimation |
3.如何在XML文件中定义动画
步骤如下:
①新建 Android 项目
②在res目录中新建anim文件夹
③在anim目录中新建一个my_anim.xml(注意文件名小写)
④在 my_anim.xml 加入动画代码
importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; importandroid.view.animation.AnimationSet; importandroid.view.animation.RotateAnimation; importandroid.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; importandroid.widget.Button; importandroid.widget.ImageView; public class Animation1Activity extends Activity { private Button rotateButton = null; private Button scaleButton = null; private Button alphaButton = null; private Button translateButton = null; private ImageView image = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rotateButton = (Button)findViewById(R.id.rotateButton); scaleButton = (Button)findViewById(R.id.scaleButton); alphaButton = (Button)findViewById(R.id.alphaButton); translateButton = (Button)findViewById(R.id.translateButton); image = (ImageView)findViewById(R.id.image); rotateButton.setOnClickListener(newRotateButtonListener()); scaleButton.setOnClickListener(newScaleButtonListener()); alphaButton.setOnClickListener(newAlphaButtonListener()); translateButton.setOnClickListener( new TranslateButtonListener()); } class AlphaButtonListener implementsOnClickListener{ public void onClick(View v) { //创建一个AnimationSet对象,参数为Boolean型, //true表示使用Animation的interpolator,false则是使用自己的 AnimationSet animationSet = new AnimationSet(true); //创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); //设置动画执行的时间 alphaAnimation.setDuration(500); //将alphaAnimation对象添加到AnimationSet当中 animationSet.addAnimation(alphaAnimation); //使用ImageView的startAnimation方法执行动画 image.startAnimation(animationSet); } } class RotateButtonListener implementsOnClickListener{ public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); //参数1:从哪个旋转角度开始 //参数2:转到什么角度 //后4个参数用于设置围绕着旋转的圆的圆心在哪里 //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标 //参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴 //参数5:确定y轴坐标的类型 //参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setDuration(1000); animationSet.addAnimation(rotateAnimation); image.startAnimation(animationSet); } } class ScaleButtonListener implementsOnClickListener{ public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); //参数1:x轴的初始值 //参数2:x轴收缩后的值 //参数3:y轴的初始值 //参数4:y轴收缩后的值 //参数5:确定x轴坐标的类型 //参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴 //参数7:确定y轴坐标的类型 //参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴 ScaleAnimation scaleAnimation = new ScaleAnimation( 0, 0.1f,0,0.1f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); scaleAnimation.setDuration(1000); animationSet.addAnimation(scaleAnimation); image.startAnimation(animationSet); } } class TranslateButtonListener implementsOnClickListener{ public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); //参数1~2:x轴的开始位置 //参数3~4:y轴的开始位置 //参数5~6:x轴的结束位置 //参数7~8:x轴的结束位置 TranslateAnimation 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); animationSet.addAnimation(translateAnimation); image.startAnimation(animationSet); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/rotateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转" /> <Button android:id="@+id/scaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放" /> <Button android:id="@+id/alphaButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="淡入淡出" /> <Button android:id="@+id/translateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="移动" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/an" /> </LinearLayout> </LinearLayout>
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
浅谈Kotlin(一):简介及Android Studio中配置
浅谈Kotlin(一):简介及Android Studio中配置 浅谈Kotlin(二):基本类型、基本语法、代码风格 浅谈Kotlin(三):类 浅谈Kotlin(四):控制流 前言: 今日新闻:谷歌宣布,将Kotlin语言作为安卓开发的一级编程语言。 Kotlin由JetBrains公司开发,与Java 100%互通,并具备诸多Java尚不支持的新特性。 谷歌称还将与JetBrains公司合作,为Kotlin设立一个非盈利基金会。 一、简介: Kotlin 是一个基于 JVM 的新的编程语言,由JetBrains开发。JetBrains,作为目前广受欢迎的 Java IDEIntelliJ的提供商,在 Apache 许可下已经开源其Kotlin 编程语言。 可以理解为类似于iOS的Swift。 二、特性: 轻量级: 这一点对于Android来说非常重要。项目所需要的库应该尽可能的小。Android对于方法数量有严格的限制,Kotlin只额外增加了大约6000个方法。 互操作: Kotlin可与Java语言无缝通信。这意味着我们可以在Kotlin代码中使用任何...
- 下一篇
我写的这些opensource项目
将自己在googlecode和github上的所有项目过了一遍,整理一张列表,列下一些还有点价值和用处的项目,都不是什么great job,纯粹是为了工作需要或者乐趣写的东西,看官要是有兴趣也可以瞧瞧。 一 Java相关 1.Xmemcached,还算是比较多人使用的一个java memcached client,优点是效率和易用性,缺点是代码写的不怎么样,两年前发展到现在的东西,以后还会继续维护。 2.HS4J,看handlersocket的时候顺手写的客户端,我们公司内部某些项目在用,可能还有其他公司外的朋友在用,后来同事聚石贡献了一个扩展项目hs4j-kit,更易于使用,他写的代码很优雅漂亮,推荐一看。暂时没有精力维护。 3.Aviator,一个很初级的表达式执行引擎,行家看到肯定要笑话我。不过语法上很符合我自己的口味,我们自己的项目在用,也有几个朋友在用,会继续维护。 4.Jevent,一个玩具,其实是模仿libevent的一个java实现,对nio或者libevent的实现机制感兴趣的还可以看看。 5. Kilim,我fork的kilim实现,修改了nio调度器,使用多个re...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7,8上快速安装Gitea,搭建Git服务器
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS关闭SELinux安全模块
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Hadoop3单机部署,实现最简伪集群
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7安装Docker,走上虚拟化容器引擎之路