这一篇来使用逐帧动画和补间动画来实现一个小样例,首先我们来看看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. struct获取不到值的小错误

    struct2 get set 这两个方法一定要用双骆驼命名法:getA() setA(),        而geta() seta()不行 我找了好久的错误,只能说框架这东西快捷方便,找起错误要人命

  2. tensorflow note

    #!/usr/bin/python # -*- coding: UTF- -*- # @date: // : # @name: first_tf_1223 # @author:vickey-wu fr ...

  3. phpStrom+xdebug调试php

    1>xdebug下载 1.1>xdebug官网可以根据phpinfo()源代码来提供对应版本的xdebug,地址:https://xdebug.org/wizard.php 如下截图 1. ...

  4. xenserver tools 安装

    mkdir -p /mnt/xtools mount /dev/cdrom /mnt/xtools cd /mnt/xtools/Linux/ ./install.sh -n init 6

  5. 如何用纯 CSS 创作一种文字断开的交互特效

    效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/LmjNgL 可交互视频教程 此视 ...

  6. 基于tiny4412的u-boot移植(二)(转)

    http://www.cnblogs.com/pengdonglin137/archive/2015/12/27/5080645.html

  7. python-with管理文件上下文(基本文件操作)

    什么是文件 文件是操作系统为用户提供的一个读写硬盘的虚拟单位,文件的操作就是文件的读.写. 操作过程:当我们双击文件 -<- 操作系统接收到指令请求(将用户或应用程序的读写操作转换成集体的硬盘指 ...

  8. Feedback on Ch5 paper based on CFD-RANS

    It is encouraging that you took the initiative to write this journal manuscript, but it needs a lot ...

  9. LeetCode(21)Merge Two Sorted Lists

    题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  10. Selenium学习系列---- FirePath的安装和使用

    在用Selenium编写测试用例的时候,需要对对网页元素上定位,而现在很多的浏览器是可以看到网页上相关的元素信息,可以查看某一个网页的元素信息,通过定位的方式查找元素.另外安装好Selenium ID ...