很多手机应用的引导页都是动画的,添加动画后的应用画面会更加生动灵活,今天博主也学习了Android中Animation的使用,下面来总结下。 
  android中的Animation分为两种,一种是Frame Animation逐帧动画,一种是Tween Animation补间动画。

Frame Animation逐帧动画

  逐帧动画,顾名思义就是定义画面播放的每一帧画面,然后Android按照顺序依次显示。逐帧动画与放电影的原理是相同的。下面我们来看逐帧动画实现的具体步骤: 
1. 首先在res文件下建立一个anim的文件夹,这个文件夹是专门用于存放Animation相关的xml文件的。 
2. 在anim文件夹下创建一个animation_frame.xml的文件。该文件用于存放播放的每一帧画面。

  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
  2.  
  3. <item android:drawable="@mipmap/aa" android:duration="500"/>
  4. <item android:drawable="@mipmap/bb" android:duration="500"/>
  5. <item android:drawable="@mipmap/cc" android:duration="500"/>
  6. <item android:drawable="@mipmap/dd" android:duration="500"/>
  7. <item android:drawable="@mipmap/ee" android:duration="500"/>
  8. <item android:drawable="@mipmap/ff" android:duration="500"/>
  9. <item android:drawable="@mipmap/gg" android:duration="500"/>
  10. <item android:drawable="@mipmap/hh" android:duration="500"/>
  11.  
  12. </animation-list>

  

  <animation-list></animation-list>为动画的标签,我们在这个标签内通过定义<item/>项,来定义动画显示的每一个画面。<animation-list > 标签中android:oneshot="false" 这是一个非常重要的属性,默认为false 表示 动画循环播放, 如果这里写true 则表示动画只播发一次。<item/>标签内的android:drawable用来定义画面图片,android:duration定义画面的显示时长。

3. 在Activity的xml布局中定义两个按钮用于开始和停止播放控制,一个ImageView用于显示动画。在ImageView中通过设置设置背景引入我们刚才写的animation_frame.xml动画文件。

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context="com.example.administrator.myanimation.FrameAnimationDemo">
  7.  
  8. <Button
  9. android:id="@+id/button_play"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="开始播放" />
  13.  
  14. <Button
  15. android:id="@+id/button_stop"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="停止播放" />
  19.  
  20. <ImageView
  21. android:id="@+id/imageview_frame"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:background="@anim/animation_frame" />
  25. </LinearLayout>

4. 在Activity中获得布局中相关控件的对象,并定义动画,具体如下:

  1. public class FrameAnimationDemo extends Activity implements View.OnClickListener {
  2. private Button mButtonPlay;//是定义停止按钮
  3. private Button mButtonStop;//定义开始按钮
  4. //定义显示动画的ImageView
  5. private ImageView mImageViewShow;
  6. //定义逐帧动画
  7. private AnimationDrawable animationDrawable;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_frame_animation_demo);
  13. mButtonPlay = (Button) findViewById(R.id.button_play);//开始按钮
  14. mButtonStop = (Button) findViewById(R.id.button_stop);//停止按钮
  15. //获得ImageView的对象
  16. mImageViewShow = (ImageView) findViewById(R.id.imageview_frame);
  17. //通过ImageView获得逐帧动画的对象
  18. animationDrawable = (AnimationDrawable) mImageViewShow.getBackground();
  19. //设置点击事件
  20. mButtonPlay.setOnClickListener(this);
  21. mButtonStop.setOnClickListener(this);
  22. }
  23. @Override
  24. public void onClick(View v) {
  25. switch (v.getId()) {
  26. case R.id.button_play:
  27. Log.d("data", "开始播放");
  28. animationDrawable.start();//开始播放
  29. break;
  30. case R.id.button_stop:
  31. Log.d("data", "开始播放");
  32. animationDrawable.stop();//停止播放
  33. break;
  34. default:
  35. break;
  36. }
  37. }
  38. }

  逐帧动画使用AnimationDrawanle,通过ImageView对象的getBackgroud()方法获得AnimationDrawanle对象,然后调用start()方法开始播放动画,调用stop()方法停止播放动画。当然在逐帧动画还可以通过addFrame(Drawable frame, int duration)方法添加每一帧的画面,而不是定义animation_frame.xml文件。

AnimationDrawanle就是用来控制逐帧动画的,他还提供了很多其他的方法进行操作(以下是常用的一部分):

start(); 开始这个动画 
stop(); 结束这个动画 
setAlpha(100);设置动画的透明度, 取值范围(0 - 255) 
setOneShot(true); 设置单次播放 
setOneShot(false); 设置循环播放 
isRunning(); 判断动画是否正在播放 
getNumberOfFrames(); 得到动画的帧数。

Tween Animation补间动画

  补间动画与逐帧动画的区别是补间动画不用自己去定义动画的每一帧变化,我们只需要指定动画的第一帧和最后一帧的关键帧即可,其中间的中间帧由系统自己去计算。 
  Tween Animation动画中可以定义动画移动、放大、缩小以及产生透明度的变化,我们自己指定动画的两个关键帧以及动画的变化方式,然后系统自己去计算动画的中间变化和轨迹,也就是我们动画的中间过程全是由系统帮我们完成的。这样导致了一个缺点就是我们不容一区控制动画的变化,所以在很多游戏的开发中不会用到补间动画。下面,我们来学习一下补间动画的使用。 
  补间动画Tween Animation通过使用Animation来使用,我们首先来看一下Animation: 

Animation包含5个子类: 
AlphaAnimation:透明度改变的动画,通过指定动画开始的透明度和结束的透明度,以及动画 维持的时间。其中透明度是从0到1定义的。 
RotateAnimation:旋转的动画,通过指定动画开始时的旋转角度和结束时的旋转角度来定义动画的旋转。 
ScaleAnimation:缩放的动画,通过指定动画X,Y轴的缩放,来对动画进行缩放。 
TranslateAnimation :平移的动画,通过定义动画在X, Y轴上的平移量来定义动画的平移。 
AnimationSet:混合的动画,将以上动画效果混合形成的动画效果。

  下面我们分别来看5种动画效果的实现,首先定义一个Activity的布局文件,在布局文件中定义5个按钮分别实现 AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation 动画,定义一个ImageView,显示动画效果。

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity">
  6.  
  7. <LinearLayout
  8. android:id="@+id/layout1"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:gravity="center">
  12.  
  13. <Button
  14. android:id="@+id/button_start_alpha"
  15. android:layout_width="0dp"
  16. android:layout_weight="1"
  17. android:layout_height="wrap_content"
  18. android:text="透明动画" />
  19. <Button
  20. android:id="@+id/button_start_translate"
  21. android:layout_width="0dp"
  22. android:layout_weight="1"
  23. android:layout_height="wrap_content"
  24. android:text="平移动画" />
  25. <Button
  26. android:id="@+id/button_start_rotate"
  27. android:layout_width="0dp"
  28. android:layout_weight="1"
  29. android:layout_height="wrap_content"
  30. android:text="旋转动画" />
  31. <Button
  32. android:id="@+id/button_start_scale"
  33. android:layout_width="0dp"
  34. android:layout_weight="1"
  35. android:layout_height="wrap_content"
  36. android:text="缩放动画" />
  37.  
  38. </LinearLayout>
  39. <LinearLayout
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:layout_below="@+id/layout1">
  43. <Button
  44. android:id="@+id/button_start_set"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:text="混合动画"/>
  48. </LinearLayout>
  49.  
  50. <ImageView
  51. android:id="@+id/imageview"
  52. android:layout_width="100dp"
  53. android:layout_height="100dp"
  54. android:background="@mipmap/ic_launcher"
  55. android:layout_gravity="center"
  56. android:layout_centerInParent="true"/>
  57.  
  58. </RelativeLayout>

AlphaAnimation:

1. 创建一个AlphaAnimation的对象。AlphaAnimation有如下构造器:

  • AlphaAnimation(Context context, AttributeSet attrs) 
      该构造器由于加载res文件夹anim文件夹中的xml配置时使用。
  • AlphaAnimation(float fromAlpha, float toAlpha) 
      该构造器用于直接定义该动画变化的透明度,从fromAlpha变化到toAlpha。范围为0到1。 
       
      这里我们使用第二个构造器。 
    2. 设置AlphaAnimation对象动画的显示时间。通过调用setDuration(long durationMillis)方法。 
    3. ImageView对象调用startAnimation(Animation animation)方法开始显示动画。
  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId()){
  4. case R.id.button_start_alpha:
  5. mImageView.setImageResource(R.mipmap.ic_launcher);//设置画面显示
  6. //透明动画
  7. AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
  8. animation.setDuration(5000);//动画播放5秒
  9. mImageView.startAnimation(animation);
  10. break;
  11. default:
  12. break;
  13. }
  14. }

TranslateAnimation:

  在平移动画按钮的点击事件中定义如下: 
1. 创建一个TranslateAnimation的对象。TranslateAnimation有如下构造器:

  • TranslateAnimation(Context context, AttributeSet attrs) 
      该构造器由于加载res文件夹anim文件夹中的xml配置时使用。
  • TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 
      该构造器用于直接定义该动画变化的位移,X轴从fromXDelta变化到toXDelta,Y轴从fromYDelta变化到toYDelta。
  • TranslateAnimation (int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) 
      该构造器也是用于直接定义该动画变化的位移,fromXType是指后面fromXValue的数值是现对于自己的还是相对于父布局的,它有两个值:Animation.RELATIVE_TO_SELFAnimation.RELATIVE_TO_PARENT。后面的也是如此,坐标的变化是:X轴从fromXValue变化到toXValue,Y轴从fromYValue变化到toYValue。

      这里我们使用第二个构造器。 
    2. 设置TranslateAnimation 对象动画的显示时间。通过调用setDuration(long durationMillis)方法。 
    3. ImageView对象调用startAnimation(Animation animation)方法开始显示动画。

  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId()){
  4. case R.id.button_start_alpha:
  5. mImageView.setImageResource(R.mipmap.ic_launcher);//设置画面显示
  6. TranslateAnimation animationTranslate = new TranslateAnimation(0, 2 * mImageView.getMeasuredWidth(), 0,
  7. 2 * mImageView.getMeasuredHeight());
  8. animationTranslate.setDuration(5000);//动画播放5秒
  9. mImageView.startAnimation(animationTranslate);
  10. break;
  11. default:
  12. break;
  13. }
  14. }

RotateAnimation :

  在旋转动画按钮的点击事件中定义如下: 
1. 创建一个RotateAnimation 的对象。RotateAnimation 有如下构造器:

  • RotateAnimation(Context context, AttributeSet attrs) 
      该构造器由于加载res文件夹anim文件夹中的xml配置时使用。
  • RotateAnimation(float fromDegrees, float toDegrees) 
      该构造器用于直接定义该动画旋转的角度,从fromDegrees变化到toDegrees。
  • RotateAnimation (float fromDegrees, float toDegrees, float pivotX, float pivotY) 
      该构造器用于直接定义该动画旋转的角度和旋转的中心,以(pivotX,pivotY)为中心从fromDegrees变化到toDegrees。
  • RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
      该构造器用于直接定义该动画旋转的角度和旋转的中心,以(pivotXValue,pivotYValue)为中心从fromDegrees变化到toDegrees。旋转中心的定义有两种类型:Animation.RELATIVE_TO_SELFAnimation.RELATIVE_TO_PARENT

      这里我们使用第二个构造器。 
    2. 设置RotateAnimation 对象动画的显示时间。通过调用setDuration(long durationMillis)方法。 
    3. ImageView对象调用startAnimation(Animation animation)方法开始显示动画。

  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId()){
  4. case R.id.button_start_alpha:
  5. mImageView.setImageResource(R.mipmap.ic_launcher);//设置画面显示
  6. //旋转动画
  7. RotateAnimation animationRoate = new RotateAnimation(0, 360);
  8. animationTranslate.setDuration(5000);//动画播放5秒
  9. mImageView.startAnimation(animationTranslate);
  10. break;
  11. default:
  12. break;
  13. }
  14. }

ScaleAnimation:

  在缩放动画按钮的点击事件中定义如下: 
1. 创建一个ScaleAnimation的对象。ScaleAnimation有如下构造器:

  • ScaleAnimation(Context context, AttributeSet attrs) 
      该构造器由于加载res文件夹anim文件夹中的xml配置时使用。
  • ScaleAnimation(float fromX, float toX, float fromY, float toY) 
      该构造器用于直接定义该动画缩放的大小,X轴从fromX变化到toX,Y轴从fromY变化到toY。
  • ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) 
      该构造器用于直接定义该动动画缩放的大小和缩放的中心,以(pivotX,pivotY)为中心缩放。
  • ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
      该构造器用于直接定义该动动画缩放的大小和缩放的中心,以(pivotX,pivotY)为中心缩放。缩放中心的定义有两种类型:Animation.RELATIVE_TO_SELFAnimation.RELATIVE_TO_PARENT

      这里我们使用第二个构造器。 
    2. 设置ScaleAnimation对象动画的显示时间。通过调用setDuration(long durationMillis)方法。 
    3. ImageView对象调用startAnimation(Animation animation)方法开始显示动画。

  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId()){
  4. case R.id.button_start_alpha:
  5. mImageView.setImageResource(R.mipmap.ic_launcher);//设置画面显示
  6. //缩放动画
  7. ScaleAnimation animationScale = new ScaleAnimation(0.0f, 1f, 0.0f, 1f);
  8. animationTranslate.setDuration(5000);//动画播放5秒
  9. mImageView.startAnimation(animationTranslate);
  10. break;
  11. default:
  12. break;
  13. }
  14. }

AnimationSet:

  在混合动画按钮的点击事件中定义如下: 
1. 创建一个AnimationSet的对象。AnimationSet有如下构造器:

  • AnimationSet(Context context, AttributeSet attrs) 
      该构造器由于加载res文件夹anim文件夹中的xml配置时使用。
  • AnimationSet (boolean shareInterpolator) 
      是否共享Interpolator的设置。

      这里我们使用第二个构造器。 
    2. 设置AnimationSet对象动画的显示时间。通过调用setDuration(long durationMillis)方法。 
    3. ImageView对象调用startAnimation(Animation animation)方法开始显示动画。

  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId()){
  4. case R.id.button_start_alpha:
  5. mImageView.setImageResource(R.mipmap.ic_launcher);//设置画面显示
  6. AnimationSet animationSet= new AnimationSet(false);
  7.  
  8. //从透明到不透明
  9. AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
  10. //平移
  11. TranslateAnimation animation2 = new TranslateAnimation(0,mImageView.getMeasuredWidth(), 0,mImageView.getMeasuredHeight());
  12. //旋转
  13. RotateAnimation animation3 = new RotateAnimation(0, 360);
  14. //缩放
  15. ScaleAnimation animation4 = new ScaleAnimation(0.0f, 1f, 0.0f, 1f);
  16. //为混合动画添加效果
  17. animationSet.addAnimation(animation1);
  18. animationSet.addAnimation(animation2);
  19. animationSet.addAnimation(animation3);
  20. animationSet.addAnimation(animation4);
  21.  
  22. animationSet.setDuration(10000);//动画播放10秒
  23. animationTranslate.setDuration(5000);//动画播放5秒
  24. mImageView.startAnimation(animationTranslate);
  25. break;
  26. default:
  27. break;
  28. }
  29. }

XML设置动画:

  有时候我们会感觉在Java代码中设置代码会使代码看起来更加复杂和庞大,那么为了减少java代码的附中,我们可以将对于动画的设置提升成一个xml文件。这里我们以缩旋转为例定义: 
1. 首先在res文件夹下建立一个anim文件夹。 
2. 在anim文件夹下创建一个animation_rotate.xml文件。 
  这里定义了动画的时间,开始角度,结束角度,以及旋转的中心和重复的次数。注意:repeatCount值为:-1时,循环旋转;0时, 旋转一次;1时旋转2次……以此类推。

  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_decelerate_interpolator">
  4. <rotate
  5. android:duration="3000"
  6. android:fromDegrees="0"
  7. android:pivotX="50%"
  8. android:pivotY="50%"
  9. android:repeatCount="1"
  10. android:toDegrees="360"></rotate>
  11. </set>

3. 在Activity中加载animation_rotate.xml文件,AnimationUtils.loadAnimation()方法加载 
  使用AnimationUtils.loadAnimation()方法加载返回一个Animation的对象。

  1. //使用在AnimationUtils.loadAnimation()方法加载。
  2. Animation animationRotate = AnimationUtils.loadAnimation(this, R.anim.animation_rotate);
  3. mImageView.startAnimation(animationRotate);

Android 动画——Frame Animation与Tween Animation的更多相关文章

  1. Android动画效果之初识Property Animation(属性动画)

    前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...

  2. Android动画之二:View Animation

    作为一个博客<Android其中的动画:Drawable Animation>.android动画主要分为三大部分.上一篇博客已经解说Drawable Animation的使用方法,即逐帧 ...

  3. android动画介绍之 自己定义Animation动画实现qq抖一抖效果

    昨天我们介绍了Animation的基本使用方法.小伙伴们了解的怎么样了?假设还没有了解过Animation的小伙伴能够看看这篇博客 android动画介绍--Animation 实现loading动画 ...

  4. Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)

    1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...

  5. Android动画效果之Frame Animation(逐帧动画)

    前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...

  6. Android动画效果之Tween Animation(补间动画)

    前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...

  7. Android基础夯实--重温动画(一)之Tween Animation

    心灵鸡汤:真正成功的人生,不在于成就的大小,而在于你是否努力地去实现自我,喊出自己的声音,走出属于自己的道路. 摘要 不积跬步,无以至千里:不积小流,无以成江海.学习任何东西我们都离不开扎实的基础知识 ...

  8. Android动画效果之Property Animation进阶(属性动画)

    前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...

  9. Android动画学习笔记-Android Animation

    Android动画学习笔记-Android Animation   3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...

随机推荐

  1. laravel实现excel表格导出

    记得引用一下excel,现在laravel5.2都默认自带的,不需要自己再 Composer安装依赖了. use Excel; 然后方法里这样写 //$cellData自己要进行导出的数组 Array ...

  2. Fisher Vector Encoding and Gaussian Mixture Model

    一.背景知识 1. Discriminant  Learning Algorithms(判别式方法) and Generative Learning Algorithms(生成式方法) 现在常见的模式 ...

  3. Python-快速排序

    算法思想:快速排序运用了分而治之的思想,即在所选数组中选择一个基准(任选一个都可以),以改基准为基础,将小于该基准的元素都移动基准的左边,大于该基准的数据都移动到右边,然后对左右两边进行递归处理.同样 ...

  4. Java文件流之练习

    1 )将"今年是反法西斯胜利70周年,举国欢庆,所以要放假啦" 字符串 使用文件字符输出流 写入到oldhappy.txt文件中,复写10000行, 要求换行 在文件的开头写入当前 ...

  5. C++如何入门

    去 Visual Studiohttps://www.visualstudio.com/zh-hans/?rr=https%3A%2F%2Flink.zhihu.com%2F%3Ftarget%3Dh ...

  6. Centos 执行shell命令返回127错误

    shell脚本功能:连接mysql,自动创建数据库,脚本如下 mysql -h$MYSQL_IP -u$MYSQL_USER -p$MYSQL_PASSWORD --default-character ...

  7. Webpack插件开发简要

    背景 如今'大前端'这个概念在前端界大热,说'大前端',我们就要提到'前后端分离','前后端分离'又离不开'本地开发构建','本地开发构建'自然离不开webpack,webpack想要工作,那它就需要 ...

  8. 常用linux小工具介绍

    1.ctags(Generate tag files for source code)是vim下方便代码阅读的工具.尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM. ctags 最先是 ...

  9. 用css控制字数,多余的用省略号代替

    选择器 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100px; } white-space 属性 ...

  10. Kubernets 资源类型简介

    # Node 代表 Kubernets 集群运行的宿主物理机或者虚拟服务器, 为容器提供必要的计算资源: 内存 与 CPU 等. # Pod 最底层的抽象. 一个 Pod 中可以包含一个或者多个运行的 ...