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.                        
  17.                 </Button>
  18.                 <ImageView
  19.                         android:id="@+id/iv1"
  20.                         android:layout_width="wrap_content"
  21.                         android:layout_height="wrap_content"
  22.                         android:src = "@drawable/icon"
  23.                 >
  24.                 </ImageView>
  25.                
  26.                 <Button
  27.                         android:text="scale"
  28.                         android:id="@+id/Button02"
  29.                         android:layout_width="wrap_content"
  30.                         android:layout_height="wrap_content">
  31.                        
  32.                 </Button>
  33.                 <ImageView
  34.                         android:id="@+id/iv2"
  35.                         android:layout_width="wrap_content"
  36.                         android:layout_height="wrap_content"
  37.                         android:src = "@drawable/icon"
  38.                 >
  39.                 </ImageView>
  40.                
  41.                 <Button
  42.                         android:text="rotate"
  43.                         android:id="@+id/Button03"
  44.                         android:layout_width="wrap_content"
  45.                         android:layout_height="wrap_content">
  46.                        
  47.                 </Button>
  48.                 <ImageView
  49.                         android:id="@+id/iv3"
  50.                         android:layout_width="wrap_content"
  51.                         android:layout_height="wrap_content"
  52.                         android:src = "@drawable/icon"
  53.                 >
  54.                 </ImageView>
  55.                
  56.                 <Button
  57.                         android:text="transf"
  58.                         android:id="@+id/Button04"
  59.                         android:layout_width="wrap_content"
  60.                         android:layout_height="wrap_content">
  61.                        
  62.                 </Button>
  63.                 <ImageView
  64.                         android:id="@+id/iv4"
  65.                         android:layout_width="wrap_content"
  66.                         android:layout_height="wrap_content"
  67.                         android:src = "@drawable/icon"
  68.                 >
  69.                 </ImageView>
  70.                
  71.                 <Button
  72.                         android:text="complex"
  73.                         android:id="@+id/Button05"
  74.                         android:layout_width="wrap_content"
  75.                         android:layout_height="wrap_content">
  76.                        
  77.                 </Button>
  78.                 <ImageView
  79.                         android:id="@+id/iv5"
  80.                         android:layout_width="wrap_content"
  81.                         android:layout_height="wrap_content"
  82.                         android:src = "@drawable/icon"
  83.                 >
  84.                 </ImageView>
  85.         </LinearLayout>                                                                       
  86. </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.        
  23.         ImageView iv1;
  24.         ImageView iv2;
  25.         ImageView iv3;
  26.         ImageView iv4;
  27.         ImageView iv5;
  28.        
  29.         AnimationSet as ;
  30.     @Override
  31.     public void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.main);
  34.         alphaBut = (Button)findViewById(R.id.Button01);
  35.         scaleBut = (Button)findViewById(R.id.Button02);
  36.         rotateBut = (Button)findViewById(R.id.Button03);
  37.         transfBut = (Button)findViewById(R.id.Button04);
  38.         complexBut = (Button)findViewById(R.id.Button05);
  39.         
  40.         alphaBut.setOnClickListener(this);
  41.         scaleBut.setOnClickListener(this);
  42.         rotateBut.setOnClickListener(this);
  43.         transfBut.setOnClickListener(this);
  44.         complexBut.setOnClickListener(this);
  45.         
  46.         iv1 = (ImageView)findViewById(R.id.iv1);
  47.         iv2 = (ImageView)findViewById(R.id.iv2);
  48.         iv3 = (ImageView)findViewById(R.id.iv3);
  49.         iv4 = (ImageView)findViewById(R.id.iv4);
  50.         iv5 = (ImageView)findViewById(R.id.iv5);
  51.       
  52.     }
  53.         @Override
  54.         public void onClick(View v) {
  55.                 Button b = (Button)v;
  56.                 switch(b.getId()){
  57.                 case R.id.Button01://alpha
  58.                         alphaAnimation();
  59.                         break;
  60.                 case R.id.Button02://scale
  61.                         scaleAnimation();
  62.                         break;
  63.                 case R.id.Button03://rotate
  64.                         rotateAnimation();
  65.                         break;
  66.                 case R.id.Button04 ://transf
  67.                         transfAnimation();
  68.                         break;
  69.                 case R.id.Button05 ://complex
  70.                         complexAnimation();
  71.                         break;
  72.                 }
  73.                
  74.         }
  75.         /**
  76.          * 综合例子
  77.          */
  78.         private void complexAnimation() {
  79.                 as = new AnimationSet(true);//1.实例化AnimationSet
  80.                
  81.                 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);
  82.                 ta.setDuration(5000);//设置动画的时间
  83.                
  84.                 AlphaAnimation aa = new AlphaAnimation(1, 0.3f);//2.创建需要的animation
  85.                 aa.setDuration(3000);
  86.                
  87.                 as.addAnimation(ta);//3.将animation加入AnimationSet
  88.                 as.addAnimation(aa);//3.将animation加入AnimationSet
  89.                
  90.                
  91.                 as.setFillAfter(true);//最终停止
  92.                
  93.                 complexBut.startAnimation(as);//4.开始动画
  94.                 iv5.startAnimation(as);//开始动画
  95.         }
  96.         /**
  97.          * 平移
  98.          */
  99.         private void transfAnimation() {
  100.                 as = new AnimationSet(true);//1.实例化AnimationSet
  101.                 TranslateAnimation aa = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1);
  102.                 aa.setDuration(5000);//设置动画的时间
  103.                 as.addAnimation(aa);//3.将animation加入AnimationSet
  104.                 transfBut.startAnimation(as);//4.开始动画
  105.                 iv4.startAnimation(as);//开始动画
  106.         }
  107.         /**
  108.          * 旋转
  109.          */
  110.         private void rotateAnimation() {
  111.                 as = new AnimationSet(true);//1.实例化AnimationSet
  112.                 RotateAnimation aa = new RotateAnimation(0, 270, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.2f);
  113.                 aa.setDuration(5000);//设置动画的时间
  114.                 as.addAnimation(aa);//3.将animation加入AnimationSet
  115.                 rotateBut.startAnimation(as);//4.开始动画
  116.                 iv3.startAnimation(as);//开始动画
  117.         }
  118.         /**
  119.          * 变化大小
  120.          */
  121.         private void scaleAnimation() {
  122.                 as = new AnimationSet(true);//1.实例化AnimationSet
  123. //                ScaleAnimation aa = new ScaleAnimation(1, 2, 1, 2);//2.创建需要的animation,这种构造方法是以自身为pivot
  124.                 ScaleAnimation aa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
  125.                 aa.setDuration(7000);//设置动画的时间
  126.                 as.addAnimation(aa);//3.将animation加入AnimationSet
  127.                 scaleBut.startAnimation(as);//4.开始动画
  128.                 iv2.startAnimation(as);//开始动画
  129.         }
  130.         /**
  131.          * 透明度渐变
  132.          */
  133.         private void alphaAnimation(){
  134.                 as = new AnimationSet(true);//1.实例化AnimationSet
  135.                 AlphaAnimation aa = new AlphaAnimation(1, 0);//2.创建需要的animation
  136.                 aa.setDuration(2000);//设置动画的时间
  137.                 as.addAnimation(aa);//3.将animation加入AnimationSet
  138.                 alphaBut.startAnimation(as);//4.开始动画
  139.                 iv1.startAnimation(as);//开始动画
  140.         }
  141.    
  142. }
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值