分类:C#、Android、VS2015;

创建日期:2016-03-21

一、简介

Android 提供了以下三种创建动画的方式:

  • Drawable Animations – 画板动画,也叫帧动画(Frame Animations)。
  • View Animations - 视图动画,也叫补间动画(Tween Animations)。
  • Property Animations – 属性动画。从Android 3.0开始提供。

注意:虽然这三种动画都可用,但只要有可能,都应该优先考虑用属性动画来实现。另外,动画虽然能吸引人,但不要滥用,否则只会适得其反。

1、画板动画(Drawable Animations)

Drawable Animations提供了按帧播放的简单动画API,在其他实现技术中一般将其称为帧动画(Frame Animations)。这种动画的播放效果非常类似电影或卡通(cartoon)漫画。

帧动画是通过顺序播放图片来产生动画效果的,下图通过顺序播放6张图片实现一个人跳起来的动画效果:

控制动画序列的画板资源(XML文件)通常保存在应用程序的/Resource/drawable文件夹中,文件中用<animation-list>元素作为根元素,用<item>元素定义每一帧。

例如,在/Resource/drawable/ch2103DrawableAnimDemo.xml文件中定义动画序列:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/a01" android:duration="100" />

<item android:drawable="@drawable/a02" android:duration="100" />

<item android:drawable="@drawable/a03" android:duration="100" />

<item android:drawable="@drawable/a04" android:duration="100" />

<item android:drawable="@drawable/a05" android:duration="100" />

<item android:drawable="@drawable/a06" android:duration="100" />

</animation-list>

此动画包含六个帧。其中,a01~a06是图片文件,android:duration属性声明每个帧显示的时间。定义动画序列以后,只需要在布局文件中指定该文件,Android就会自动按顺序加载和显示动画。

2、视图动画(View Animations)

在Android系统中,视图动画(或者叫补间动画)有4种表现方式:渐变、缩放、平移、旋转。利用View动画能完成一系列诸如位置、大小、不透明度、旋转等简单的变化。例如对程序中的某个ImageView实现动画处理等。

View动画位于Android.View.Animation命名空间中,在代码中可通过下面的方法对View对象进行动画处理:

AlphaAnimation:控制不透明度变化的动画类

Rotate Animation:控制旋转的动画类

ScaleAnimation:控制缩放变化的动画类

TranslateAnimation:控制平移变化的动画类

AnimationSet:控制动画属性的集合类

动画变换文件一般保存在/Resources/anim文件夹中。另外,虽然此API是早期版本提供的,但是由于它的简单性,因此仍然有用。

注意:保存在/Resources/anim文件夹中的XML文件是声明View动画的首选办法,因为这种方式更易于阅读和维护。该XML文件必须用以下元素之一作为根元素:

  • alpha -淡入或淡出动画
  • rotate -旋转动画
  • scale –缩放动画
  • translate –平移(水平或垂直运动)
  • set - 动画容器,可容纳一个或多个其他动画元素

默认情况下,Android会同时应用该XML文件中的所有动画。要使动画按指定的顺序变化,将android:startOffset属性设置在上面定义的元素之一即可。

也可以内插器改变动画速率,如加速、重复、减速等动画效果:

  • AcclerateInterpolator / DecelerateInterpolator –增加或减少动画速率
  • BounceInterpolator – 动画结束时反弹
  • LinearInterpolator – 恒定速率

具体用法见本节示例中的“视图动画示例”。在这个例子中,动画效果是先将图像沿水平和垂直方向缩放,然后将图像逆时针旋转45度同时缩小图像的大小。

3、属性动画(Property Animations)

这种动画可对任何对象的属性进行处理(包括View),是首选的执行动画的方式,即使动画对象不可见也一样能对其进行处理。

属性动画API的灵活性在于还能将动画封装在不同的类中,使代码共享更加方便。

所有属性动画都是通过Animator子类的实例来创建:

  • ValueAnimator – 这是整个属性动画API中最重要的类。它会自动计算需要更改的属性值,该动画并不是直接修改这些值,而是用事件去更新动画对象。
  • ObjectAnimator – 对目标对象的某个属性进行动画处理。
  • AnimationSet – 此类用于保存动画集合,程序中利用它可以“同时执行、顺序执行、延时执行”这些动画。

使用动画是,可能还需要下面的特殊类:

  • IntEvaluator – 计算整数类型的属性值
  • FloatEvaluator –计算浮点类型的属性值
  • ArgbEvaluator –计算颜色类型的属性值

如果正在进行动画处理的属性不是float、int或Color,可通过实现ITypeEvaluator接口创建他们自己的计算类型。

(1)ValueAnimator

通过调用下面的方法之一,可获取得ValueAnimator的实例:

  • ValueAnimator.OfInt
  • ValueAnimator.OfFloat
  • ValueAnimator.OfObject

下面的代码演示如何将值从 0 到 100进行动画处理,动画持续时间为1000毫秒。

ValueAnimator animator = ValueAnimator.OfInt(0, 100);

animator.SetDuration(1000);

animator.Start();

但是,仅有这些代码还不够,这是因为虽然执行了动画但是并没有将目标更新为新的值,因此还需要引入相关的事件:

MyCustomObject myObj = new MyCustomObject();

myObj.SomeIntegerValue = -1;

animator.Update += (object sender, ValueAnimator.AnimatorUpdateEventArgs e) =>

{

int newValue = (int) e.Animation.AnimatedValue;

// Apply this new value to the object being animated.

myObj.SomeIntegerValue = newValue;

};

(2)ObjectAnimator

ObjectAnimator是ViewAnimator的子类,它将计时引擎和ValueAnimator结合在一起实现动画。例如:

MyCustomObject myObj = new MyCustomObject();

myObj.SomeIntegerValue = -1;

ObjectAnimator animator = ObjectAnimator.OfFloat(myObj, "SomeIntegerValue”, 0, 100);

animator.SetDuration(1000);

animator.Start();

与前面的代码相比,这样做可减少代码量。

二、示例

1、运行截图

2、设计步骤

(1)添加图片

在Drawable文件夹下添加6个图片(ch2103asteroid01.png~ch2103asteroid06.png),这些图片用于演示帧动画的用法。

然后再添加一个ch2103ship.png图片,该图片用于演示视图动画的用法。

(2)添加ch2103DrawableAnimDemo.xml

在Resources/Drawable文件夹下添加该文件。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:drawable="@drawable/ch2103asteroid01" android:duration="100" />
  4. <item android:drawable="@drawable/ch2103asteroid02" android:duration="100" />
  5. <item android:drawable="@drawable/ch2103asteroid03" android:duration="100" />
  6. <item android:drawable="@drawable/ch2103asteroid04" android:duration="100" />
  7. <item android:drawable="@drawable/ch2103asteroid05" android:duration="100" />
  8. <item android:drawable="@drawable/ch2103asteroid06" android:duration="100" />
  9. </animation-list>

(3)添加ch2103ViewAnimDemo.xml

在Resources/anim文件夹下添加该文件。

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shareInterpolator="false">
  4.  
  5. <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  6. android:fromXScale="1.0"
  7. android:toXScale="1.4"
  8. android:fromYScale="1.0"
  9. android:toYScale="0.6"
  10. android:pivotX="50%"
  11. android:pivotY="50%"
  12. android:fillEnabled="true"
  13. android:fillAfter="false"
  14. android:duration="700" />
  15.  
  16. <set android:interpolator="@android:anim/accelerate_interpolator">
  17. <scale android:fromXScale="1.4"
  18. android:toXScale="0.0"
  19. android:fromYScale="0.6"
  20. android:toYScale="0.0"
  21. android:pivotX="50%"
  22. android:pivotY="50%"
  23. android:fillEnabled="true"
  24. android:fillBefore="false"
  25. android:fillAfter="true"
  26. android:startOffset="700"
  27. android:duration="400" />
  28.  
  29. <rotate android:fromDegrees="0"
  30. android:toDegrees="-45"
  31. android:toYScale="0.0"
  32. android:pivotX="50%"
  33. android:pivotY="50%"
  34. android:fillEnabled="true"
  35. android:fillBefore="false"
  36. android:fillAfter="true"
  37. android:startOffset="700"
  38. android:duration="400" />
  39. </set>
  40. </set>

(4)添加ch2103Main.axml

在Resources/layout文件夹下添加该文件。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:minWidth="25px"
  7. android:minHeight="25px">
  8. <TextView
  9. android:text="画板动画示例"
  10. android:textAppearance="?android:attr/textAppearanceSmall"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:gravity="center"
  14. android:background="@color/myGray"
  15. android:layout_marginTop="5dp" />
  16. <ImageView
  17. android:src="@android:drawable/ic_menu_gallery"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:id="@+id/ch2103_imageView_DrawableDemo"
  21. android:layout_marginTop="5dp" />
  22. <TextView
  23. android:text="视图动画示例"
  24. android:textAppearance="?android:attr/textAppearanceSmall"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:gravity="center"
  28. android:background="@color/myGray"
  29. android:layout_marginTop="5dp" />
  30. <ImageView
  31. android:src="@android:drawable/ic_menu_gallery"
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:id="@+id/ch2103_imageView_ViewDemo"
  35. android:layout_marginLeft="20dp"
  36. android:layout_marginTop="5dp" />
  37. <Button
  38. android:text="开始"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:id="@+id/ch2103_btnViewDemoStart"
  42. android:layout_gravity="center" />
  43. <TextView
  44. android:text="属性动画示例(拖放滑动条观察进度条动画)"
  45. android:textAppearance="?android:attr/textAppearanceSmall"
  46. android:layout_width="fill_parent"
  47. android:layout_height="wrap_content"
  48. android:gravity="center"
  49. android:background="@color/myGray"
  50. android:layout_marginTop="5dp" />
  51. <FrameLayout
  52. android:minWidth="25px"
  53. android:minHeight="35px"
  54. android:layout_width="fill_parent"
  55. android:layout_height="wrap_content"
  56. android:id="@+id/frameLayout1"
  57. android:layout_marginTop="15dp"
  58. android:layout_marginBottom="5dp">
  59. <MyDemos.SrcDemos.ch2103MyView
  60. android:layout_width="fill_parent"
  61. android:layout_height="wrap_content"
  62. android:layout_marginLeft="8dp"
  63. android:layout_marginRight="8dp"
  64. android:id="@+id/ch2103myview1" />
  65. </FrameLayout>
  66. <SeekBar
  67. android:layout_width="fill_parent"
  68. android:layout_height="wrap_content"
  69. android:id="@+id/ch2103seekBar1"
  70. android:max="100"
  71. android:progress="50"
  72. android:layout_marginLeft="8dp"
  73. android:layout_marginRight="8dp"
  74. android:layout_marginTop="10dp" />
  75. </LinearLayout>

(5)添加ch2103MyView.cs

  1. using System;
  2. using Android.Content;
  3. using Android.Views;
  4. using Android.Graphics;
  5. using Android.Util;
  6. using Android.Animation;
  7.  
  8. namespace MyDemos.SrcDemos
  9. {
  10. /// <summary>
  11. /// 演示进度条动画控制的基本用法
  12. /// </summary>
  13. public class ch2103MyView : View
  14. {
  15. private const int DefaultHeight = 20;
  16. private const int DefaultWidth = 120;
  17.  
  18. private Paint mNegativePaint;
  19. private double mPosition = 0.5;
  20. private Paint mPositivePaint;
  21.  
  22. public ch2103MyView(Context context, IAttributeSet attrs)
  23. : this(context, attrs, 0)
  24. {
  25. Initialize();
  26. }
  27.  
  28. public ch2103MyView(Context context, IAttributeSet attrs, int defStyle)
  29. : base(context, attrs, defStyle)
  30. {
  31. Initialize();
  32. }
  33.  
  34. public double CurrentValue
  35. {
  36. get { return mPosition; }
  37. set
  38. {
  39. mPosition = Math.Max(0f, Math.Min(value, 1f));
  40. Invalidate();
  41. }
  42. }
  43.  
  44. public void SetCurentValue(double value, bool animate)
  45. {
  46. if (!animate)
  47. {
  48. CurrentValue = value;
  49. return;
  50. }
  51. ValueAnimator animator = ValueAnimator.OfFloat((float)mPosition, (float)Math.Max(0f, Math.Min(value, 1f)));
  52. animator.SetDuration(500);
  53.  
  54. animator.Update += (sender, e) => CurrentValue = (double)e.Animation.AnimatedValue;
  55. animator.Start();
  56. }
  57.  
  58. protected override void OnDraw(Canvas canvas)
  59. {
  60. base.OnDraw(canvas);
  61. float middle = canvas.Width * (float)mPosition;
  62.  
  63. canvas.DrawPaint(mNegativePaint);
  64.  
  65. canvas.DrawRect(0, 0, middle, canvas.Height, mPositivePaint);
  66. }
  67.  
  68. protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
  69. {
  70. int width = MeasureSpec.GetSize(widthMeasureSpec);
  71. SetMeasuredDimension(width < DefaultWidth ? DefaultWidth : width, DefaultHeight);
  72. }
  73.  
  74. private void Initialize()
  75. {
  76. mPositivePaint = new Paint
  77. {
  78. AntiAlias = true,
  79. Color = Color.Rgb(0x99, 0xcc, 0),
  80. };
  81. mPositivePaint.SetStyle(Paint.Style.FillAndStroke);
  82.  
  83. mNegativePaint = new Paint
  84. {
  85. AntiAlias = true,
  86. Color = Color.Rgb(0xff, 0x44, 0x44)
  87. };
  88. mNegativePaint.SetStyle(Paint.Style.FillAndStroke);
  89. }
  90. }
  91. }

(6)添加ch2103MainActivity.cs

  1. using Android.App;
  2. using Android.OS;
  3. using Android.Widget;
  4. using Android.Views.Animations;
  5.  
  6. namespace MyDemos.SrcDemos
  7. {
  8. [Activity(Label = "【例21-3】动画基本用法")]
  9. public class ch2103MainActivity : Activity
  10. {
  11. protected override void OnCreate(Bundle savedInstanceState)
  12. {
  13. base.OnCreate(savedInstanceState);
  14.  
  15. SetContentView(Resource.Layout.ch2103Main);
  16.  
  17. //画板动画Demo
  18. var img1 = FindViewById<ImageView>(Resource.Id.ch2103_imageView_DrawableDemo);
  19. img1.SetImageResource(Resource.Drawable.ch2103DrawableAnimDemo);
  20.  
  21. //视图动画Demo
  22. var btn1 = FindViewById<Button>(Resource.Id.ch2103_btnViewDemoStart);
  23. btn1.Click += (sender, args) => {
  24. Animation animation = AnimationUtils.LoadAnimation(this, Resource.Animation.ch2103ViewAnimDemo);
  25. var img2 = FindViewById<ImageView>(Resource.Id.ch2103_imageView_ViewDemo);
  26. img2.SetImageResource(Resource.Drawable.ch2103ship);
  27. img2.StartAnimation(animation);
  28. };
  29.  
  30. //属性动画Demo
  31. var myView = FindViewById<ch2103MyView>(Resource.Id.ch2103myview1);
  32. var seekBar = FindViewById<SeekBar>(Resource.Id.ch2103seekBar1);
  33. seekBar.StopTrackingTouch += (sender, args) =>
  34. {
  35. double currentValue = ((double)seekBar.Progress) / seekBar.Max;
  36. myView.SetCurentValue(currentValue, true);
  37. };
  38. }
  39. }
  40. }

【Android】21.3 动画的更多相关文章

  1. Android 三种动画详解

    [工匠若水 http://blog.csdn.net/yanbober 转载请注明出处.点我开始Android技术交流] 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让 ...

  2. Android传统View动画与Property动画基础及比较

    前言:关于动画方面的知识也整理一段时间了,如题,这篇文章简单的介绍了View和Property动画的概念,如何在项目中创建资源文件,以及如何在代码中使用它们,本次整理动画的重点放在了Property动 ...

  3. Android立体旋转动画实现与封装(支持以X、Y、Z三个轴为轴心旋转)

    本文主要介绍Android立体旋转动画,或者3D旋转,下图是我自己实现的一个界面 立体旋转分为以下三种: 1. 以X轴为轴心旋转 2. 以Y轴为轴心旋转 3. 以Z轴为轴心旋转--这种等价于andro ...

  4. Android中矢量动画

    Android中矢量动画 Android中用<path> 标签来创建SVG,就好比控制着一支画笔,从一点到一点,动一条线. <path> 标签 支持一下属性 M = (Mx, ...

  5. Android Animation(动画)

    前言 Android 平台提供实现动画的解决方案(三种) 一.3.0以前,android支持两种动画: (1)Frame Animation:顺序播放事先做好的图像,与gif图片原理类似,是一种逐帧动 ...

  6. android 补间动画和Animation

    介绍: 补间动画是一种设定动画开始状态.结束状态,其中间的变化由系统计算补充.这也是他叫做补间动画的原因. 补间动画由Animation类来实现具体效果,包括平移(TranslateAnimation ...

  7. Android中过场动画

    overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left); 第一参数为进入的动画 第二参数为退出的动画 进入的动画 ...

  8. Android 自定义波浪动画 --"让进度浪起来~"

    原文链接:http://www.jianshu.com/p/0e25a10cb9f5 一款效果不错的动画,实现也挺简单的,推荐阅读学习~ -- 由 傻小孩b 分享 waveview <Andro ...

  9. Android自定义窗口动画

    第一步,设置出现和消失的xml 1.在res/anim下创建enter_anim.xml,设置窗口出现的动画 <?xml version="1.0" encoding=&qu ...

随机推荐

  1. Java EE HTML5 WebSocket 示例

    http://www.oschina.net/translate/java-ee-html5-websocket-example?cmp HTML5给Web浏览器带来了全双工TCP连接websocke ...

  2. Android 升级脚本updater-script 的函数简单介绍

    这是Android系统来执行updater-scripts中的函数介绍. 函数都是的Edify语言.当调用这些函数结束的时候.会返回数据给脚本.当然,你也能够使用这些函数的返回值来确认成功与否,比如: ...

  3. 【协议篇】UDP

    UDP(User Data Protocol,用户数据报协议)是与TCP相对应的协议.它是面向非连接的协议,它不与对方建立连接,而是直接就把数据包发送过去! UDP适用于一次只传送少量数据.对可靠性要 ...

  4. webservice系统学习笔记8-简单的权限校验

    服务端handler.java package com.ws01; import java.util.Set; import javax.xml.namespace.QName; import jav ...

  5. WordPress 主题教程:从零开始制作 WordPress 主题

    为什么要开发WordPress主题? WordPress主题由一系列文件和样式表单组成,这些文件和样式表单共同作用生成WordPress网站的外观.每个主题都不同,用户可以通过这些主题随心所欲地更换自 ...

  6. 使用Properties去读取配置文件,并获得具体内容值

    有时候,写了一个配置文件,需要知道读出来的内容对不对,我们需要测试一下,看看读出来的跟我们要的是不是一样.这里写了一个工具类,用来读取配置文件里面的内容. 一.使用Properties工具类来读取. ...

  7. CAS连接微软活动目录的配置方法

    原文地址:http://blog.csdn.net/baozhengw/article/details/3857669在微软活动目录中建立一个用户节点,帐号为wangzhenyu,cn为zhenyu ...

  8. 重要:VC DLL编程

    VC DLL编程 静态链接:每个应用程序使用函数库,必须拥有一份库的备份.多个应用程序运行时,内存中就有多份函数库代码的备份. 动态连接库:多个应用程序可以共享一份函数库的备份. DLL的调用方式:即 ...

  9. HDUOJ---1863畅通工程

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. scanf/sscanf %[]格式控制串的用法(转)

    scanf/sscanf %[]格式控制串的用法 scanf中一种很少见但很有用的转换字符:[...]和[ ^...]. #include<stdio.h> int main() { ch ...