这一篇来使用逐帧动画和补间动画来实现一个小样例,首先我们来看看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. RestTemplate-postForObject源码

    参数:    请求路径, 请求参数, 返回类型, 扩展模板变量

  2. gcc 变量类型大小 练习 远离 cygwin64 需要带dll

    /* testmini.c -- very simple test program for the miniLZO library */ #include <stdio.h> #inclu ...

  3. 169. Majority Element@python

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. Python能干啥?

    Python之py9 Python之py9-录音自动下载 Python之py9-py9作业检查 Python之py9-py9博客情况获取 Python之py9-微信监控获取mp3_url Python ...

  5. 04 Beautiful Soup

    Beautiful Soup 简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.py ...

  6. SpringBoot log4j2 异常

    log4j 配置 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  7. STM32--TIM定时器时钟分割(疑难)

    不太明白 (1)        TIM_Perscaler来设置预分频系数: (2)        TIM_ClockDivision来设置时钟分割(时钟分频因子): (3)        TIM_C ...

  8. 【转】阿里巴巴分布式服务框架 Dubbo 团队成员梁飞专访

    原文链接:http://www.iteye.com/magazines/103   Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,每天为2000+ 个服务提供3,000,000,000+ ...

  9. URI跟URL的区别

    关于URL和URI的区别,个人见解.    初学java,最近被一个概念搞得头晕脑胀,就是url和uri的概念和区别,网上查了一大通,发现各种回答眼花缭乱,有百科直接粘贴的,有胡说八道的,有故意绕来绕 ...

  10. hexo干货系列:(六)hexo提交搜索引擎(百度+谷歌)

    前言 能看到这里,说明大家都跟我一样,已经把博客搭起来并洋洋洒洒写了几篇博文,正春风得意感觉良好的时候,搭建博客有屎以来最大的危机出现在没有准备的我面前,百度+谷歌都无法搜索到我的博客.装逼还没几天就 ...