Android中Animation 详细解读
Animation从总体来说可以分为两类:
1.Tweened Animations:该类提供了旋转,移动,伸展,淡入淡出等效果
Tweened Animations也有四种类型:
1. Alpha:淡入淡出效果
2. Scale:缩放效果
3. Rotate:旋转效果
4. Translate:移动效果
设置动画有两种方式:在xml文件中或者在java代码中
在XML中设置动画效果步骤:
1. 在res文件夹下新建一个名为anim的文件夹
2. 创建xml文件,并首先加入set标签(set标签就相当于Java代码中的AnimationSet)
3. 在Set标签中加入alpha,scale,rotate,translate标签(相当于Java代码中的AlphaAnimation,ScaleAnimation,RotateAnimation,TranslateAnimation)
4. 在Java代码中使用AnimationUtils的loadAnimation方法来加载XML文件,并得到一个Animation对象
5. 使用控件的startAnimation()方法执行这个Animation对象
那么通用的属性:
- android:duration:设置动画持续时间
- android:fillAfter:如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态
- android:fillBefore:如果fillBefore设为true,则动画执行后,控件将回到动画开始的状态
- android:startOffset(long startOffset):设置动画执行之前等待的时间(单位:毫秒)
- android:repeatCount(int repeatCount):设置动画重复的次数
- android:interpolator:设置动画的变化速度,其值: {
android:interpolator="@android:anim/accelerate_decelerate_interpolator":先加速,后减速
android:interpolator="@android:anim/accelerate_interpolator":加速
android:interpolator="@android:anim/decelerate_interpolator":减速
android:interpolator="@android:anim/cycle_Interpolator":动画循环播放特定次数,速率改变沿着正弦曲线
android:interpolator="@android:anim/linear_Interpolator":匀速
}
示例:
- 1. <?xml version="1.0" encoding="utf-8"?>
- 2. <set xmlns:android="http://schemas.android.com/apk/res/android"
- 3. android:interpolator="@android:anim/accelerate_interpolator" >
- 4.
- 5. <alpha
- 6. android:duration="500"
- 7. android:fromAlpha="1.0"
- 8. android:startOffset="500"
- 9. android:toAlpha="0.0" />
- 10.
- 11. </set>
1. <?xml version="1.0" encoding="utf-8"?>
2. <set xmlns:android="http://schemas.android.com/apk/res/android"
3. android:interpolator="@android:anim/accelerate_interpolator" >
4.
5. <alpha
6. android:duration="500"
7. android:fromAlpha="1.0"
8. android:startOffset="500"
9. android:toAlpha="0.0" />
10.
11. </set>
rotate.xml
- 1. <?xml version="1.0" encoding="utf-8"?>
- 2. <set xmlns:android="http://schemas.android.com/apk/res/android"
- 3. android:interpolator="@android:anim/accelerate_interpolator" >
- 4.
- 5. <rotate
- 6. android:duration="5000"
- 7. android:fromDegrees="0"
- 8. android:pivotX="50%"
- 9. android:pivotY="50%"
- 10. android:toDegrees="360" />
- 11.
- 12. </set>
1. <?xml version="1.0" encoding="utf-8"?>
2. <set xmlns:android="http://schemas.android.com/apk/res/android"
3. android:interpolator="@android:anim/accelerate_interpolator" >
4.
5. <rotate
6. android:duration="5000"
7. android:fromDegrees="0"
8. android:pivotX="50%"
9. android:pivotY="50%"
10. android:toDegrees="360" />
11.
12. </set>
scale.xml
- 1. <?xml version="1.0" encoding="utf-8"?>
- 2. <set xmlns:android="http://schemas.android.com/apk/res/android"
- 3. android:interpolator="@android:anim/accelerate_interpolator" >
- 4.
- 5. <scale
- 6. android:duration="2000"
- 7. android:fromXScale="1.0"
- 8. android:fromYScale="1.0"
- 9. android:pivotX="50%"
- 10. android:pivotY="50%"
- 11. android:toXScale="0.0"
- 12. android:toYScale="0.0" />
- 13.
- 14. </set>
1. <?xml version="1.0" encoding="utf-8"?>
2. <set xmlns:android="http://schemas.android.com/apk/res/android"
3. android:interpolator="@android:anim/accelerate_interpolator" >
4.
5. <scale
6. android:duration="2000"
7. android:fromXScale="1.0"
8. android:fromYScale="1.0"
9. android:pivotX="50%"
10. android:pivotY="50%"
11. android:toXScale="0.0"
12. android:toYScale="0.0" />
13.
14. </set>
translate.xml
- 1. <?xml version="1.0" encoding="utf-8"?>
- 2. <set xmlns:android="http://schemas.android.com/apk/res/android"
- 3. android:interpolator="@android:anim/accelerate_interpolator" >
- 4.
- 5. <translate
- 6. android:duration="2000"
- 7. android:fromXDelta="50%"
- 8. android:fromYDelta="0%"
- 9. android:toXDelta="100%"
- 10. android:toYDelta="100%" />
- 11.
- 12. </set>
1. <?xml version="1.0" encoding="utf-8"?>
2. <set xmlns:android="http://schemas.android.com/apk/res/android"
3. android:interpolator="@android:anim/accelerate_interpolator" >
4.
5. <translate
6. android:duration="2000"
7. android:fromXDelta="50%"
8. android:fromYDelta="0%"
9. android:toXDelta="100%"
10. android:toYDelta="100%" />
11.
12. </set>
使用:
- case R.id.alphaButton:
- Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
- mImageView.startAnimation(alphaAnimation);
- break;
- case R.id.scaleButton:
- Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);
- mImageView.startAnimation(scaleAnimation);
- break;
- case R.id.rotateButton:
- Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
- mImageView.startAnimation(rotateAnimation);
- break;
- case R.id.translateButton:
- Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
- mImageView.startAnimation(translateAnimation);
- break;
case R.id.alphaButton:
Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
mImageView.startAnimation(alphaAnimation);
break; case R.id.scaleButton:
Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);
mImageView.startAnimation(scaleAnimation);
break; case R.id.rotateButton:
Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
mImageView.startAnimation(rotateAnimation);
break; case R.id.translateButton:
Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
mImageView.startAnimation(translateAnimation);
break;
java代码实现
Java代码中的通用属性:
- setDuration(long durationMillis):设置动画持续事件(单位:毫秒)
- setFillAfter(boolean fillAfter):如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态
- setFillBefore(boolean fillBefore):如果fillBefore设为true,则动画执行后,控件将回到动画开始的状态
- setStartOffset(long startOffset):设置动画执行之前等待的时间(单位:毫秒)
- setRepeatCount(int repeatCount):设置动画重复的次数
- setInterpolator(Interpolator i):设置动画的变化速度
setInterpolator(newAccelerateDecelerateInterpolator()):先加速,后减速
setInterpolator(newAccelerateInterpolator()):加速
setInterpolator(newDecelerateInterpolator()):减速
setInterpolator(new CycleInterpolator()):动画循环播放特定次数,速率改变沿着正弦曲线
setInterpolator(new LinearInterpolator()):匀速
以及其他一些特定的动画效果
java代码
- case R.id.alphaButton:
- // 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
- AnimationSet animationSet = new AnimationSet(true);
- // 创建一个AlphaAnimation对象(参数表示从完全不透明到完全透明)
- AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
- // 设置动画执行的时间(单位:毫秒)
- alphaAnimation.setDuration(1000);
- // 将AlphaAnimation对象添加到AnimationSet当中
- animationSet.addAnimation(alphaAnimation);
- // 使用ImageView的startAnimation方法开始执行动画
- mImageView.startAnimation(animationSet);
- break;
- case R.id.scaleButton:
- // 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
- animationSet = new AnimationSet(true);
- // 创建一个ScaleAnimation对象(以某个点为中心缩放)
- ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
- Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- // 设置动画执行之前等待的时间(单位:毫秒)
- scaleAnimation.setStartOffset(1000);
- // 设置动画执行的时间(单位:毫秒)
- scaleAnimation.setDuration(2000);
- // 如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态
- // 运行了一下发现以下奇怪的现象
- // scaleAnimation.setFillAfter(true);不会停留在动画结束的状态
- // animationSet.setFillAfter(true);则会停留在动画结束的状态
- animationSet.setFillAfter(true);
- // 将ScaleAnimation对象添加到AnimationSet当中
- animationSet.addAnimation(scaleAnimation);
- // 使用ImageView的startAnimation方法开始执行动画
- mImageView.startAnimation(animationSet);
- break;
- case R.id.rotateButton:
- // 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
- animationSet = new AnimationSet(true);
- // 创建一个RotateAnimation对象(以某个点为圆心旋转360度)
- RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
- Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.25f);
- // 设置动画执行的时间(单位:毫秒)
- rotateAnimation.setDuration(5000);
- // 将RotateAnimation对象添加到AnimationSet当中
- animationSet.addAnimation(rotateAnimation);
- // 使用ImageView的startAnimation方法开始执行动画
- mImageView.startAnimation(animationSet);
- break;
- case R.id.translateButton:
- // 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
- animationSet = new AnimationSet(true);
- // 创建一个RotateAnimation对象(从某个点移动到另一个点)
- TranslateAnimation translateAnimation = new TranslateAnimation(
- Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f);
- // 设置动画执行的时间(单位:毫秒)
- translateAnimation.setDuration(1000);
- // 将TranslateAnimation对象添加到AnimationSet当中
- animationSet.addAnimation(translateAnimation);
- // 使用ImageView的startAnimation方法开始执行动画
- mImageView.startAnimation(animationSet);
- break;
Android中Animation 详细解读的更多相关文章
- Android BLE蓝牙详细解读
代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...
- wemall app商城源码Android中ViewHolder详细解释
1.ViewHolder的解释: (1).只是一个静态类,不是Android的API方法. (2).它的作用就在于减少不必要的调用findViewById,然后把对底下的控件引用存在ViewHolde ...
- 关于Android中Animation的停止【转载】
转载自:http://blog.csdn.net/easonx1990/article/details/8231520 最近遇到一个需求,通过在GridView上改变焦点,并且GridView上每个i ...
- android中Animation动画的连续播放与播放完毕后停留在最后的状态
我们做安卓应用的苦逼程序员们常常会需要用到Animation也就是动画.比如做地图功能的时候.我们在手机旋转时需要根据手机重力感应来调整地图的角度,让它上面的“北”一直指向地球的北面...好多人做动画 ...
- PHP中foreach详细解读
oreach 语法结构提供了遍历数组的简单方式.foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息.有两种语法: foreach (array_ ...
- Android中apk动态载入技术研究(2)android插件化及实现
了解了android中类载入的前期知识点后,来看看android中DexClassLoader详细的实现 详细载入流程例如以下: 宿主程序会到文件系统比方SD卡中去载入APK[1],然 ...
- Android中的GraphicBuffer同步机制-Fence
Fence是一种同步机制,在Android里主要用于图形系统中GraphicBuffer的同步.那它和已有同步机制相比有什么特点呢?它主要被用来处理跨硬件的情况.尤其是CPU.GPU和HWC之间的同步 ...
- 详细解读Android中的搜索框(二)—— Search Dialog
Search Dialog是提供搜索的控件之一,还有一个是上次小例子给出的searchView,关于SearchView的东西后面会说到.本次先从Search Dialog说起,让大家慢慢理解andr ...
- 深入Animation,在SurfaceView中照样使用Android—Tween Animation!
第一类:Frame By Frame 帧动画( 不推荐游戏开发中使用) 所谓帧动画,就是顺序播放事先做好的图像,类似于放电影: 分析: 此种方式类似我之 ...
随机推荐
- android 百度地图 通过剪裁图片添加 Marker
初始化百度地图: private void initViews() { mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mM ...
- 读书笔记:javascript高级技巧(一)
一.安全的类型检测 javascript内置的类型检测机制并非完全可靠,由于浏览器或者作用域等原因,经常会发生错误.大家知道,在任何值调用toString()方法都会返回一个[object Nativ ...
- To Do List
妈呀...发现不发博文公布自己要学的东西压力少太多了.......... 然后就会变得颓废..................... 求大家监督QAQ....To Do List是近3天左右目标,3天 ...
- 我的Linux对拍脚本
本文用于Linux下bash的对拍脚本: brute为本目录的暴力程序.. pro为优化过的程序 mak造数据的.. #!/bin/bash while(true)do ./mak printf &q ...
- HDU 3652 B-number(数位DP)
题目链接 学习大神的数位DP模版. #include <iostream> #include <cstdio> #include <cstring> using n ...
- Eclipse IDE for C/C++ Developers安装配置详解
Eclipse IDE for C/C++ Developers安装配置详解(转) 转自:http://hi.baidu.com/ltb6w/item/986532efd712460f570f1ddc ...
- Git初使用
今天开始初次使用Git,Git作为一个使用广泛的分布式版本控制系统,我们有必要熟悉掌握. 这次主要是实现将本地上的“Hello World”的完整的项目文件提交到github上新建的代码库,主要过程如 ...
- thinkphp上传至服务器后模板无法解析原因
前几日做好的响应式静态页面上传至虚拟空间,打开网址地址出现: 模板不存在:./app/Home/View/Index/index.html 错误位置 FILE: /home/u333385714/pu ...
- POJ 1195 二维树状数组
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18489 Accepted: 8558 De ...
- SQL sp_executesql【转】
execute相信大家都用的用熟了,简写为exec,除了用来执行存储过程,一般都用来执行动态Sql sp_executesql,sql2005中引入的新的系统存储过程,也是用来处理动态sql的, 如: ...