这一篇来使用逐帧动画和补间动画来实现一个小例子,首先我们来看看Android中的补间动画。
Android中使用Animation代表抽象的动画类,该类包括下面几个子类:
AlphaAnimation:透明改变动画。
ScaleAnimation:大小缩放动画。
TranslateAnimation:位移变化动画。
RotateAnimation:旋转动画。
我们下面使用位移动画和逐帧动画来实现这个小例子,先看看运行效果:

蝴蝶挥动翅膀的逐帧动画文件:
- <?xmlversion="1.0"encoding="utf-8"?>
- <!-- 定义动画循环播放 -->
- <animation-listxmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- <itemandroid:drawable="@drawable/butterfly_f01"android:duration="120"/>
- <itemandroid:drawable="@drawable/butterfly_f02"android:duration="120"/>
- <itemandroid:drawable="@drawable/butterfly_f03"android:duration="120"/>
- <itemandroid:drawable="@drawable/butterfly_f04"android:duration="120"/>
- <itemandroid:drawable="@drawable/butterfly_f05"android:duration="120"/>
- <itemandroid:drawable="@drawable/butterfly_f06"android:duration="120"/>
- </animation-list>
界面布局文件:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/background"
- >
- <ImageView
- android:id="@+id/butterfly"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@anim/butteryfly"
- />
- </LinearLayout>
具体逻辑及位移动画:

- package com.example.butteryfly;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.Display;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.TranslateAnimation;
- import android.widget.ImageView;
- publicclass MainActivity extends Activity {
- privatefloat curX = 0;
- privatefloat curY = 30;
- privatefloat nextX = 0;
- privatefloat nextY = 0;
- privateint windowW = 0;
- privateint windowH = 0;
- private ImageView imageView;
- Handler handler = new Handler(){
- publicvoid handleMessage(android.os.Message msg) {
- if(msg.what == 0x123){
- if(nextX > windowW || nextY > windowH){
- curX = nextX = 0;
- }else{
- nextX += 8;
- }
- nextY = curY + (float) (Math.random() * 20 - 10);
- TranslateAnimation anim = new TranslateAnimation(curX, nextX, curY, nextY);
- curX = nextX;
- curY = nextY;
- anim.setDuration(200);
- imageView.startAnimation(anim);
- }
- };
- };
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = (ImageView) findViewById(R.id.butterfly);
- Display display = getWindowManager().getDefaultDisplay();
- windowW = display.getWidth();
- windowH = display.getHeight();
- final AnimationDrawable butterfly = (AnimationDrawable) imageView.getBackground();
- imageView.setOnClickListener(new OnClickListener() {
- @Override
- publicvoid onClick(View arg0) {
- butterfly.start();
- new Timer().schedule(new TimerTask() {
- @Override
- publicvoid run() {
- handler.sendEmptyMessage(0x123);
- }
- }, 0, 200);
- }
- });
- }
- }
更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-08/105023p3.htm
