`
dengbaoleng
  • 浏览: 1131685 次
文章分类
社区版块
存档分类
最新评论

Android Animation 高手必读 之一 Tweened Animations 代码实现

阅读更多

Android提供了两种动画的机制,可以通过SurfaceView来一帧一帧的绘制,同样也可以通过Animation机制。

Animations分类

Animations分为两种机制:Tweened Animations和Frame-by-FrameAnimations。

Tweened类似于flash,通过旋转、移动、缩放等实现动画效果,而Frame-by-FrameAnimations是类似于电影,通过改变每一帧的显示来实现动画效果。

其实Animation就是先写好对象要发生的动作,比如说平移,变形等。然后你需要哪个View或者View的子类,来完成这些预定义好的动作,只要给他们添加,就可以了。这部分的内容还是蛮多的,但是相对简单,本节只介绍Tweened Animations。

Tweened Animations

如图1所示,Tweened Animations有如下四种方式实现动画,对于每一种方式,Android都提供了一个类来实现。[url=]AlphaAnimation[/url], [url=]AnimationSet[/url], [url=]RotateAnimation[/url], [url=]ScaleAnimation[/url], [url=]TranslateAnimation[/url] 。

111.jpg

2011-9-30 12:35:17 上传
下载附件 (6.66 KB)

图1 Tweened Animations四种方式实现动画

对于每一种,笔者都会给出例子来详细解释。出于由简到难的考虑,首先介绍Alpha。

Alpha

alpha这个词详细如果有过opengl 开发经验的朋友一定不会陌生,没错,他就是设置透明度的。让我们先看一组图标,这样你就会加深对alpha的认识。如图2所示,实现了透明度变化的图标和按钮的效果。


222.jpg

2011-9-30 12:35:13 上传
下载附件 (17.33 KB)

图2 颜色渐变demo

如果要实现view的透明度变化,就要去实现alpha 的Animation。具体方法分为如下4部分:

q 实例化AnimationSet对象

q 实例化Animation对象

q 设置Animation对象属性

q 为View添加AnimationSet

这里要强调的就是Animation的构造方法如下:

AlphaAnimation aa = new AlphaAnimation(1, 0);//2.创建需要的animation

相对来说,alpha的构造方法最为简单。其第一个参数是出示时的透明度,0为完全透明,1为不透明。第二个参数为动画结束时的透明度。

aa.setDuration(2000);//设置动画的时间

通过上面这句可以设置动画播放的时间。

具体代码我会在最后给出。

Scale

scale为缩放,在了解Scale之前我们先看看其运行效果。如图3所示。


333.jpg

2011-9-30 12:35:14 上传
下载附件 (30.44 KB)

图3 scale demo

以上是Scale最简单的构造方法:

ScaleAnimation aa = new ScaleAnimation(1, 2, 1, 2);//2.创建需要的animation,这种构造方法是以自身为pivot

q 第1个参数为初始时x的大小,其中1为自身长度,同一2为自身长度的2倍,下同

q 第2个参数为结束动画时x的大小

q 第3个参数为初始时y的大小

q 第4个参数为结束动画时y的大小

如果你以为你已经学会了Scale,那我只能说你是浅尝辄止。下面介绍其另一个构造方法,该方法更加灵活。

ScaleAnimation aa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);

q 其四个参数同上,不解释

q 第五个参数是设置枢轴,可以是以自身,亦可以使用父view的,甚至可以谁用绝对数值。这里之后会解释。如果不明白可先忽略。

q 第六个参数是设置x枢轴的坐标。

q 接下来的两个参数同上,是设置y坐标的。

也许这里读者肯定不会明白枢轴的意思,那么我就用上面的方法来构造Animation并运行看看效果。请看图4.


444.jpg

2011-9-30 12:35:15 上传
下载附件 (47.83 KB)

图4 scale Demo2

如图所示,View控件在自身大小变化的同时,位置也在改变。后面的几个参数是设置其初始位置。如果以父view为参考,值为0.5f的话,那么就是当前父View的中间。

读者可能会问到,怎么不是屏幕的中心位置,我认为他是根据布局文件来决定位置的,当前的布局文件为LinearLayout,其设置为向下一次添加内容,如果设置y坐标为0.5f,则Android会认为你是要在当前布局接下来添加内容的位置的y轴中间。

Transfer

如果明白了枢轴的概念,那么Transfer平移就很简单了,如图5所示,为平移的例子。


555.jpg
2011-9-30 12:35:16 上传
下载附件 (32.76 KB)

图5 平移 demo

以上平移的实例化方法如下:

TranslateAnimation aa = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1);

读者结合api,相信理解这个方法应该还是比较容易的,这里就不在赘述,如果有问题,可以给留言。

Rotate

旋转,这个也是比较容易,看演示,如图6所示。


666.jpg

2011-9-30 12:35:16 上传
下载附件 (35.43 KB)

图6 旋转demo

其构造方法如下:

RotateAnimation aa = new RotateAnimation(0, 270, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.2f);

综合

Animation不但可以单独使用,还可以设置多个Animation然后添加到AnimationSet中。在对View调用,在例子中我给出了一个综合的例子,读者自行查看,检测自己是否已经明白这块知识。

代码

本节参考AnimationDemo1例子。

限于本人水平有限,如有不足之处,请多多指正。

Java代码
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. >
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. >
  11. <Button
  12. android:text="alpha"
  13. android:id="@+id/Button01"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content">

  16. </Button>
  17. <ImageView
  18. android:id="@+id/iv1"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:src = "@drawable/icon"
  22. >
  23. </ImageView>

  24. <Button
  25. android:text="scale"
  26. android:id="@+id/Button02"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content">

  29. </Button>
  30. <ImageView
  31. android:id="@+id/iv2"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:src = "@drawable/icon"
  35. >
  36. </ImageView>

  37. <Button
  38. android:text="rotate"
  39. android:id="@+id/Button03"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content">

  42. </Button>
  43. <ImageView
  44. android:id="@+id/iv3"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:src = "@drawable/icon"
  48. >
  49. </ImageView>

  50. <Button
  51. android:text="transf"
  52. android:id="@+id/Button04"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content">

  55. </Button>
  56. <ImageView
  57. android:id="@+id/iv4"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:src = "@drawable/icon"
  61. >
  62. </ImageView>

  63. <Button
  64. android:text="complex"
  65. android:id="@+id/Button05"
  66. android:layout_width="wrap_content"
  67. android:layout_height="wrap_content">

  68. </Button>
  69. <ImageView
  70. android:id="@+id/iv5"
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:src = "@drawable/icon"
  74. >
  75. </ImageView>
  76. </LinearLayout>
  77. </ScrollView>
复制代码


Java代码
  1. package cn.edu.heut.zcl;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.view.animation.AlphaAnimation;
  7. import android.view.animation.Animation;
  8. import android.view.animation.AnimationSet;
  9. import android.view.animation.RotateAnimation;
  10. import android.view.animation.ScaleAnimation;
  11. import android.view.animation.TranslateAnimation;
  12. import android.widget.Button;
  13. import android.widget.ImageButton;
  14. import android.widget.ImageView;

  15. public class DemoActivity extends Activity implements OnClickListener{
  16. /** Called when the activity is first created. */
  17. Button alphaBut ;
  18. Button scaleBut ;
  19. Button rotateBut ;
  20. Button transfBut ;
  21. Button complexBut ;

  22. ImageView iv1;
  23. ImageView iv2;
  24. ImageView iv3;
  25. ImageView iv4;
  26. ImageView iv5;

  27. AnimationSet as ;
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. alphaBut = (Button)findViewById(R.id.Button01);
  33. scaleBut = (Button)findViewById(R.id.Button02);
  34. rotateBut = (Button)findViewById(R.id.Button03);
  35. transfBut = (Button)findViewById(R.id.Button04);
  36. complexBut = (Button)findViewById(R.id.Button05);

  37. alphaBut.setOnClickListener(this);
  38. scaleBut.setOnClickListener(this);
  39. rotateBut.setOnClickListener(this);
  40. transfBut.setOnClickListener(this);
  41. complexBut.setOnClickListener(this);

  42. iv1 = (ImageView)findViewById(R.id.iv1);
  43. iv2 = (ImageView)findViewById(R.id.iv2);
  44. iv3 = (ImageView)findViewById(R.id.iv3);
  45. iv4 = (ImageView)findViewById(R.id.iv4);
  46. iv5 = (ImageView)findViewById(R.id.iv5);

  47. }
  48. @Override
  49. public void onClick(View v) {
  50. Button b = (Button)v;
  51. switch(b.getId()){
  52. case R.id.Button01://alpha
  53. alphaAnimation();
  54. break;
  55. case R.id.Button02://scale
  56. scaleAnimation();
  57. break;
  58. case R.id.Button03://rotate
  59. rotateAnimation();
  60. break;
  61. case R.id.Button04 ://transf
  62. transfAnimation();
  63. break;
  64. case R.id.Button05 ://complex
  65. complexAnimation();
  66. break;
  67. }

  68. }
  69. /**
  70. * 综合例子
  71. */
  72. private void complexAnimation() {
  73. as = new AnimationSet(true);//1.实例化AnimationSet

  74. TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0);
  75. ta.setDuration(5000);//设置动画的时间

  76. AlphaAnimation aa = new AlphaAnimation(1, 0.3f);//2.创建需要的animation
  77. aa.setDuration(3000);

  78. as.addAnimation(ta);//3.将animation加入AnimationSet
  79. as.addAnimation(aa);//3.将animation加入AnimationSet


  80. as.setFillAfter(true);//最终停止

  81. complexBut.startAnimation(as);//4.开始动画
  82. iv5.startAnimation(as);//开始动画
  83. }
  84. /**
  85. * 平移
  86. */
  87. private void transfAnimation() {
  88. as = new AnimationSet(true);//1.实例化AnimationSet
  89. TranslateAnimation aa = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1);
  90. aa.setDuration(5000);//设置动画的时间
  91. as.addAnimation(aa);//3.将animation加入AnimationSet
  92. transfBut.startAnimation(as);//4.开始动画
  93. iv4.startAnimation(as);//开始动画
  94. }
  95. /**
  96. * 旋转
  97. */
  98. private void rotateAnimation() {
  99. as = new AnimationSet(true);//1.实例化AnimationSet
  100. RotateAnimation aa = new RotateAnimation(0, 270, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.2f);
  101. aa.setDuration(5000);//设置动画的时间
  102. as.addAnimation(aa);//3.将animation加入AnimationSet
  103. rotateBut.startAnimation(as);//4.开始动画
  104. iv3.startAnimation(as);//开始动画
  105. }
  106. /**
  107. * 变化大小
  108. */
  109. private void scaleAnimation() {
  110. as = new AnimationSet(true);//1.实例化AnimationSet
  111. // ScaleAnimation aa = new ScaleAnimation(1, 2, 1, 2);//2.创建需要的animation,这种构造方法是以自身为pivot
  112. ScaleAnimation aa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
  113. aa.setDuration(7000);//设置动画的时间
  114. as.addAnimation(aa);//3.将animation加入AnimationSet
  115. scaleBut.startAnimation(as);//4.开始动画
  116. iv2.startAnimation(as);//开始动画
  117. }
  118. /**
  119. * 透明度渐变
  120. */
  121. private void alphaAnimation(){
  122. as = new AnimationSet(true);//1.实例化AnimationSet
  123. AlphaAnimation aa = new AlphaAnimation(1, 0);//2.创建需要的animation
  124. aa.setDuration(2000);//设置动画的时间
  125. as.addAnimation(aa);//3.将animation加入AnimationSet
  126. alphaBut.startAnimation(as);//4.开始动画
  127. iv1.startAnimation(as);//开始动画
  128. }

  129. }
复制代码

分享到:
评论

相关推荐

    Tweened Animations

    这是一个动画示例程序 实现淡入淡出, 缩放, 移动, 旋转 具体介绍请参见 http://write.blog.csdn.net/postedit/6406466

    Android 中 Tweened animation的实例详解

    Android 中 Tweened animation的实例详解 Tweened animation有四种类型,下面主要介绍Scale类型。 运行效果如下: Android SDK提供了2种方法:直接从XML资源中读取Animation,使用Animation子类的构造函数来初始化...

    Android编程之Animation动画详解

    本文实例讲述了Android编程之Animation动画用法。分享给大家供大家参考,具体如下: Animations 一、Animations介绍 Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行...

    Android学习之笔记---Animation的使用

    Animation从总体来说可以分为两类: Tweened Animations:该类提供了旋转,移动,...Frame-By-Frame Animations:该类可以创建一个Drawable序列,这些Drawable可以按照指定的事件间隔一个一个显示,和动画片差不多。

    ViewAnimationDemo

    ViewAnimation Tweened Animations学习

    Android编程实现3D滑动旋转效果的方法

    这里我们通过代码实现一些滑动翻页的动画效果。 Animation实现动画有两个方式:帧动画(frame-by-frame animation)和补间动画(tweened animation) 本示例通过继承Animation自定义Rotate3D,实现3D翻页效果。效果...

    Animation:实现补间动画效果

    Animation 实现补间动画效果 Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。 Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。

    Android动画入门教程之kotlin

    在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(frame-by-frame animation)和补间动画(tweened animation)。逐帧动画的工作原理很...

    Android属性动画

    源码通过属性动画实现了以前Tweened Animation的所有效果,但它还支持自己特有的动画形式,比如backgroundColor,而这是Tweened Animation无法完成的。

    详谈Android动画效果translate、scale、alpha、rotate

    动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画...Android动画模式 ...Animation主要有两种动画模式: ...一种是tweened

    Android中主要资源文件及文件夹介绍

    2:res文件夹里面的多个文件夹的各自介绍 res/anim/ XML文件,它们被编译进逐帧动画(frame by frame animation)或补间动画(tweened animation)对象 res/drawable/ .png、.9.png、.jpg文件,它们被编译进以下的...

    Android补间动画小实例

    这是一个Android的补间动画小实例 ,大家可以进行参考

Global site tag (gtag.js) - Google Analytics