这一篇来使用逐帧动画和补间动画来实现一个小例子,首先我们来看看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">
<ImageViewandroid: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);}};};@Overrideprotected 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() {@Overridepublic void onClick(View arg0) {butterfly.start();new Timer().schedule(new TimerTask() {@Overridepublic void run() {handler.sendEmptyMessage(0x123);}}, 0, 200);}});}
}

转载于:https://www.cnblogs.com/lanzhi/p/6469028.html

Android中的动画详解系列【2】——飞舞的蝴蝶相关推荐

  1. Android 中malloc_debug 原理详解

    版本基于:Android R 关联博文: Android 中malloc_debug 使用详解 0. 前言 最近上项目中遇到一个native 可能内存泄漏的问题,曾考虑使用HWASAN,但这个工具是针 ...

  2. Android中layout过程详解 (结合Android 4.0.4 最新源码)

    上一篇文章Android中mesure过程详解 (结合Android 4.0.4 最新源码)介绍了View树的measure过程,相对与measure过程,本文介绍的layout过程要简单多了,正如l ...

  3. android调webview的方法,Android中的WebView详解

    Android中的WebView详解 WebView详解 基本用法 布局文件配置WebView android:id="@+id/wv_news_detail" android:l ...

  4. Android中mesure过程详解 (结合Android 4.0.4 最新源码)

    如何遍历并绘制View树?之前的文章Android中invalidate() 函数详解(结合Android 4.0.4 最新源码)中提到invalidate()最后会发起一个View树遍历的请求,并通 ...

  5. Android中的动画详析-kotlin的demo

    Android中的动画可以分为三种,View动画,帧动画,以及属性动画,实际上帧动画也是View动画的一种,只不过二者表现形式不同,View动画是通过不断地对场景里的动画做图像转换从而产生动画效果是一 ...

  6. Android中shape属性详解

    一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用. 1.新建shape文件 首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.x ...

  7. Android实现蝴蝶动画,Android中的动画具体解释系列——飞舞的蝴蝶

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

  8. 【Android】 Android中Log调试详解

    LOG类: public final class Log extends Object java.lang.Object   android.util.Log Constants int ASSERT ...

  9. android之frame动画详解

    上一篇我们说了android中的tween动画,这一篇我们说说frame动画,frame动画主要是实现了一种类似于gif动画的效果,就是多张图按预先设定好的时间依次连续显示. 新建一个android项 ...

最新文章

  1. Redis数据介绍与指令大全
  2. NLP≠NLU,机器学习无法理解人类语言
  3. HTML5学习笔记(十八):闭包
  4. arm中的.a文件如何产生的_可变文件系统:如何在IPFS中处理文件?
  5. 各浏览器内核与C#WinForm开发Demo
  6. Detectron-MaskRCnn:Mask判别和获取前向Mask的标签
  7. 解决Eclipse 项目报错:Unbound classpath container
  8. 史上最全的 SQL 优化方案!建议收藏
  9. Qt工作笔记-QCustomPlot的基本使用
  10. 03-27 耗电量测试
  11. Java知多少(76)语言包(java.lang)简介
  12. 产品级垃圾文本分类器
  13. virtual studio 2017 配置vistual assist激活
  14. vue电商后台管理项目总结
  15. node mysql 坑_菜鸟Node.js MySQL教程遇到的坑
  16. python实践日记二
  17. 实验室信息管理系统(LIMS)软件大盘点
  18. 忍得住清贫 耐得住寂寞 禁得起诱惑
  19. golang go-redis和redigo的不同用法
  20. 第2章 获得文本语料和词汇资源

热门文章

  1. 国内流行的两大开源.net微信公众平台SDK对比分析
  2. Oracle VM VirtualBox 随系统自动启动虚拟机的方法
  3. android实现翻书效果(文本和图片的翻书)
  4. observer 观察者模式
  5. 把解压缩版的tomcat6注册成服务并设置自启动
  6. 启用IIS的Gzip压缩功能
  7. 蒋本珊计算机组成原理知识点笔记,计算机组成原理习题答案解析(蒋本珊)
  8. mysql 字段有正负值的时候 sum无效_京东金融数据分析:MySQL+HIVE的结合应用案例详解...
  9. 闪迪u盘量产工具万能版_我身边的“闪迪色”闪迪彩色手机U盘系列| 大家测573...
  10. 父元素浮动子元素会浮动吗_为什么quot;overflow:hiddenquot;能清除浮动的影响