码迷,mamicode.com
首页 > 移动开发 > 详细

Android Animation学习

时间:2015-04-22 09:46:53      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:android   animation   动画   

关于动画的实现,android提供了Animation,有两种Animation(动画)模式


1. Tween Animation:通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生动画效果,即是一种渐变动画;
2. Frame Animation:顺序播放事先做好的图像,是一种画面转换动画。

动画类型:

Android的animation由四种类型组成
在XML文件中:
|--alpha    渐变透明度变化效果
|--scale    渐变尺寸伸缩动画效果
|--translate   画面转换位置移动动画效果
|--rotate   画面转移旋转动画效果

在android源码中定义的相关的类,可以实用这些类的方法来获取和操作相关的属性
Animation
|--AlphaAnimation渐变透明度动画效果
|--ScaleAnimation渐变尺寸伸缩动画效果
|--TranslateAnimation画面转换位置移动动画效果
|--RotateAnimation画面转移旋转动画效果


下面具体讲解一下一个图片旋转的具体实例

1、首先在XML文件中定义一个图片
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id = "@+id/image_rotate"
android:src="@drawable/xuanyi"
android:layout_gravity="center"/>

2、新建一个XML文件定义rotate旋转效果
在res下新建一个名为anim的文件夹,在该文件夹下新建rotate.xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate  
android:fromDegrees = "0"  
android:toDegrees="359"
android:duration="400"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%" >

<!--android:fromDegrees 起始的角度度数-->
<!--android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针-->
<!--android:duration 表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。-->
<!--android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止-->
<!--android:pivotX 旋转中心的X坐标-->
<!--ndroid:pivotY 旋转中心的Y坐标-->
<!--其他资源可以去API中查找-->

</rotate>
</set>

3、建立Animation类的一个对象

//用AnimationUtils这个工具类加载XML文件,该方法返回一个Animation对象
Animation animaDemo = AnimationUtils.loadAnimation(this, R.anim.rotate);  
/*An interpolator defines the rate of change of an animation.
This allows the basic animation effects (alpha, scale, translate, rotate) to be accelerated, decelerated, repeated, etc.
Interpolator是一个接口,用来定义动画改变的速度,旗下有5个实现类,分别为AccelerateDecelerateInterpolator, 
AccelerateInterpolator, CycleInterpolator, DecelerateInterpolator, LinearInterpolator
LinearInterpolator为匀速效果,AccelerateInterpolator为加速效果、DecelerateInterpolator为减速效果
这里是设置了一个匀速的效果

interpolator表示变化率,但不是运行速度。一个插补属性,可以将动画效果设置为加速,减速,反复,反弹等。默认为开始和结束慢中间快,

*/
LinearInterpolator lin = new LinearInterpolator();  
//设置该动画的速度,传入一个速度变化率
animaDemo.setInterpolator(lin);  

4、在android代码中制定图片加入动画

//通过ID找到图片
imageRotate = (ImageView)findViewById(R.id.image_rotate);
//给图片加旋转的特技(动画)
imageRotate.startAnimation(animaDemos);

5、停止动画

animaDemo.clearAnimation();  

参考资料:

http://blog.csdn.net/mengzhengjie/article/details/9674845

http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html

Android Animation学习

标签:android   animation   动画   

原文地址:http://blog.csdn.net/u013675234/article/details/45176143

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!