这一篇来使用逐帧动画和补间动画来实现一个小样例,首先我们来看看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. 小程序02 wxml和wxss

    微信小程序的排版就跟wxml和wxss有关,它们两者相当于HTML和CSS,其中wxml指定了界面的框架结构,而wxss指定了界面的框架及元素的显示样式. 一.wxml 界面结构wxmL比较容易理解, ...

  2. [JOYOI] 1124 花店橱窗

    题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目背景 xq和他的老婆xz最近开了一家花店,他们准备把店里最好看的花都摆在橱窗里.但是他们 ...

  3. vue 页面过渡效果

    App.vue 模板 <template> <div id="app"> <transition :name="transition&quo ...

  4. Java 常用集合笔记

    自增数组 ArrayList<Integer>G[]=new ArrayList[N] 详细笔记 相关题目 栈 Stack<Integer> stack=new Stack&l ...

  5. 【HIHOCODER 1526】 序列的值(二进制DP)

    时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个长度为 n 的序列 a[1..n],定义函数 f(b[1..m]) 的值为在 [0,m-1] 内满足如下条件的 i ...

  6. Redis 主从复制与哨兵

    Redis 可以使用从属服务器来实现读写分离提高吞吐量或在主服务器故障时接替主服务器以提高可用性. 每个 Redis 服务器实例都可以配置多个 slave 节点,slave 服务器也可以拥有次级 sl ...

  7. idea 中的svn的使用

    http://www.cnblogs.com/whc321/p/5669804.html 很详细

  8. luogu1447 [NOI2010]能量采集

    考虑暴力,答案显然是 \(\sum_{i=1}^n\sum_{j=1}^m(2(\gcd(i,j)-1)+1)=\sum_{i=1}^n\sum_{j=1}^m(2\gcd(i,j)-1)\). 考虑 ...

  9. Ural 1960 Palindromes and Super Abilities

    Palindromes and Super Abilities Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged ...

  10. [luoguP2073] 送花(set)

    传送门 set #include <set> #include <cstdio> #include <iostream> #define LL long long ...