Android中的动画详解系列【2】——飞舞的蝴蝶

转载自:http://blog.csdn.net/dawanganban/article/details/24348227

这一篇来使用逐帧动画和补间动画来实现一个小例子,首先我们来看看Android中的补间动画。

Android中使用Animation代表抽象的动画类,该类包括下面几个子类:

AlphaAnimation:透明改变动画。

ScaleAnimation:大小缩放动画。

TranslateAnimation:位移变化动画。

RotateAnimation:旋转动画。

我们下面使用位移动画和逐帧动画来实现这个小例子,先看看运行效果:



蝴蝶挥动翅膀的逐帧动画文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 定义动画循环播放 -->  
  3. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:oneshot="false">  
  5.     <item android:drawable="@drawable/butterfly_f01" android:duration="120" />  
  6.     <item android:drawable="@drawable/butterfly_f02" android:duration="120" />  
  7.     <item android:drawable="@drawable/butterfly_f03" android:duration="120" />  
  8.     <item android:drawable="@drawable/butterfly_f04" android:duration="120" />  
  9.     <item android:drawable="@drawable/butterfly_f05" android:duration="120" />  
  10.     <item android:drawable="@drawable/butterfly_f06" android:duration="120" />                                                                  
  11. </animation-list>  
界面布局文件:

  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="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/background"  
  7.     >  
  8. <ImageView  
  9.     android:id="@+id/butterfly"  
  10.     android:layout_width="wrap_content"   
  11.     android:layout_height="wrap_content"   
  12.     android:background="@anim/butteryfly"  
  13.     />  
  14. </LinearLayout>  

具体逻辑及位移动画:


  1. package com.example.butteryfly;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import android.app.Activity;  
  7. import android.graphics.drawable.AnimationDrawable;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.view.Display;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.view.animation.TranslateAnimation;  
  14. import android.widget.ImageView;  
  15.   
  16. public class MainActivity extends Activity {  
  17.       
  18.     private float curX = 0;  
  19.     private float curY = 30;  
  20.       
  21.     private float nextX = 0;  
  22.     private float nextY = 0;  
  23.       
  24.     private int windowW = 0;  
  25.     private int windowH = 0;  
  26.       
  27.     private ImageView imageView;  
  28.       
  29.     Handler handler = new Handler(){  
  30.         public void handleMessage(android.os.Message msg) {  
  31.             if(msg.what == 0x123){  
  32.                 if(nextX > windowW || nextY > windowH){  
  33.                     curX = nextX = 0;  
  34.                 }else{  
  35.                     nextX += 8;  
  36.                 }  
  37.                   
  38.                 nextY = curY + (float) (Math.random() * 20 - 10);  
  39.                 TranslateAnimation anim = new TranslateAnimation(curX, nextX, curY, nextY);  
  40.                 curX = nextX;  
  41.                 curY = nextY;  
  42.                 anim.setDuration(200);  
  43.                 imageView.startAnimation(anim);  
  44.             }  
  45.         };  
  46.     };  
  47.     @Override  
  48.     protected void onCreate(Bundle savedInstanceState) {  
  49.         super.onCreate(savedInstanceState);  
  50.         setContentView(R.layout.activity_main);  
  51.           
  52.         imageView = (ImageView) findViewById(R.id.butterfly);  
  53.           
  54.         Display display = getWindowManager().getDefaultDisplay();   
  55.         windowW = display.getWidth();  
  56.         windowH = display.getHeight();  
  57.           
  58.         final AnimationDrawable butterfly = (AnimationDrawable) imageView.getBackground();  
  59.         imageView.setOnClickListener(new OnClickListener() {  
  60.               
  61.             @Override  
  62.             public void onClick(View arg0) {  
  63.                 butterfly.start();  
  64.                 new Timer().schedule(new TimerTask() {  
  65.                       
  66.                     @Override  
  67.                     public void run() {  
  68.                         handler.sendEmptyMessage(0x123);  
  69.                     }  
  70.                 }, 0200);  
  71.             }  
  72.         });  
  73.     }  
  74. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值