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

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

AlphaAnimation:透明改变动画。

ScaleAnimation:大小缩放动画。

TranslateAnimation:位移变化动画。

RotateAnimation:旋转动画。

我们以下使用位移动画和逐帧动画来实现这个小样例。先看看执行效果:

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

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义动画循环播放 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/butterfly_f01" android:duration="120" />
<item android:drawable="@drawable/butterfly_f02" android:duration="120" />
<item android:drawable="@drawable/butterfly_f03" android:duration="120" />
<item android:drawable="@drawable/butterfly_f04" android:duration="120" />
<item android:drawable="@drawable/butterfly_f05" android:duration="120" />
<item android:drawable="@drawable/butterfly_f06" android:duration="120" />
</animation-list>

界面布局文件:

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns: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; public class MainActivity extends Activity { private float curX = 0;
private float curY = 30; private float nextX = 0;
private float nextY = 0; private int windowW = 0;
private int windowH = 0; private ImageView imageView; Handler handler = new Handler(){
public void 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
protected void 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
public void onClick(View arg0) {
butterfly.start();
new Timer().schedule(new TimerTask() { @Override
public void run() {
handler.sendEmptyMessage(0x123);
}
}, 0, 200);
}
});
}
}

Android中的动画具体解释系列【2】——飞舞的蝴蝶的更多相关文章

  1. Android中的动画具体解释系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自己定义动画.这一篇我们来看看怎样将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 ...

  2. Android中的动画具体解释系列【3】——自己定义动画研究

    在上一篇中我们使用到了位移动画TranslateAnimation,以下我们先来看看TranslateAnimation是怎样实现Animation中的抽象方法的: /* * Copyright (C ...

  3. Android中的动画具体解释系列【1】——逐帧动画

    逐帧动画事实上非常easy,以下我们来看一个样例: <?xml version="1.0" encoding="utf-8"?> <anima ...

  4. Android中的动画详解系列【4】——Activity之间切换动画

    前面介绍了Android中的逐帧动画和补间动画,并实现了简单的自定义动画,这一篇我们来看看如何将Android中的动画运用到实际开发中的一个场景--Activity之间跳转动画. 一.定义动画资源 如 ...

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

    这一篇来使用逐帧动画和补间动画来实现一个小例子,首先我们来看看Android中的补间动画. Android中使用Animation代表抽象的动画类,该类包括下面几个子类: AlphaAnimation ...

  6. Android中的动画详解系列【3】——自定义动画研究

    在上一篇中我们使用到了位移动画TranslateAnimation,下面我们先来看看TranslateAnimation是如何实现Animation中的抽象方法的: /* * Copyright (C ...

  7. Android中的动画详解系列【1】——逐帧动画

    逐帧动画其实很简单,下面我们来看一个例子: <?xml version="1.0" encoding="utf-8"?> <animation ...

  8. Android中矢量动画

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

  9. Android中的动画

    Android中的动画分为: 1.逐帧动画(Frame Animation):  把动画过程的每张静态图片都收集起来,然后由Android来控制依次显示这些静态图片,然后利用人眼”视觉暂留“的原理,给 ...

随机推荐

  1. POJ-1163 递推

    代码很容易看明白,就不详解了. 这个是空间优化的代码. #include <iostream> #include <algorithm> #define MAX 101 usi ...

  2. oracle函数笔记(1)

    1.在数据库中显示当前时间:函数(sysdate) select sysdate from dual: 2.数据库中显示 :年/月/日 时/分/秒 ---函数:to_char(字段,'yyyy-mm- ...

  3. LeetCode(18)4Sum

    题目 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  4. python基础——6(字符编码,文件操作)

    今日内容: 1.字符编码: 人识别的语言与机器识别的语言转化的媒介 *****     2.字符与字节: 字符占多少字节,字符串转化 ***     3.文件操作: 操作硬盘中的一块区域:读写操作  ...

  5. [SQL]数据库中对值为数字,存储格式为varchar类型的字段进行排序

    如果要对数据库中某存储数字的列(存储类型不为int)进行排序,可以在order by 里对该列进行转换, 即如 order by cast(mycolumn as int) desc

  6. *lucene索引_的删除和更新

    [删除] [恢复删除] [强制删除] [优化和合并] [更新索引] 附: 代码: IndexUtil.java: package cn.hk.index; import java.io.File; i ...

  7. 如何使用jmeter进行并发登录测试

    第一种方案直接从数据库中获取账号和密码 1.设置线程数为20 ,我们的并发用户量就是20个用户同时登录 2.添加定时器 3.设置集合点,当用户数量达到20个的时候再同时请求进行登录操作 4.添加配置元 ...

  8. python010 Python3 元组

    Python3 元组Python 的元组与列表类似,不同之处在于元组的元素不能修改.元组使用小括号,列表使用方括号.元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可.如下实例: tup1 = ...

  9. zoj 2932 The Seven Percent Solution

    The Seven Percent Solution Time Limit: 2 Seconds      Memory Limit: 65536 KB Uniform Resource Identi ...

  10. CodeForces 606C--Sorting Railway Cars,思路题~~~

    C - Sorting Railway Cars   Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...