Android动画之Frame动画实战
Android动画分为Tween动画和Frame动画,Tween动画主要包括图片的放大缩小、旋转、透明度变化、移动等等操作;Frame动画则简单得多了,就是把一张张的图片连续播放产生动画效果。
本节主要介绍一下Frame动画,Tween动画会在后面的文章中介绍,敬请关注。
Frame动画主要是通过AnimationDrawable类来实现的,它有start()和stop()两个重要的方法来启动和停止动画。Frame动画一般通过XML文件配置,在工程的res/anim目录下创建一个XML配置文件,该配置文件有一个<animation-list>根元素和若干个<item>子元素。
实现一个人跳舞的Frame动画,6张图片如下所示:
1、把这6张图片放到res/drawable目录下,分别取名为:p01.png,p02.png,p03.png,p04.png,p05.png,p06.png。
2、在res/anim目录下创建一个XML配置文件,文件名为:dance.xml,文件内容:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:apk="http://schemas.android.com/apk/res/android" apk:oneshot="false">
<item apk:drawable="@drawable/p01" apk:duration="500" />
<item apk:drawable="@drawable/p02" apk:duration="500" />
<item apk:drawable="@drawable/p03" apk:duration="500" />
<item apk:drawable="@drawable/p04" apk:duration="500" />
<item apk:drawable="@drawable/p05" apk:duration="500" />
<item apk:drawable="@drawable/p06" apk:duration="500" />
</animation-list>
apk:oneshot指示是否只运行一次,设置为false则意味着循环播放。
3、在res/layout目录下创建layout配置文件dance.xml,文件内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:apk="http://schemas.android.com/apk/res/android" apk:orientation="vertical" apk:layout_width="fill_parent" apk:layout_height="fill_parent">
<!-- Frame动画图片 -->
<ImageView apk:id="@+id/ImgDance" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:background="@anim/dance" />
<!-- 动画控制按钮 -->
<LinearLayout apk:layout_width="fill_parent" apk:layout_height="wrap_content" apk:orientation="horizontal">
<Button apk:text="开始" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onStartDance" />
<Button apk:text="结束" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onStopDance" />
</LinearLayout>
</LinearLayout>
apk:background使用上面的动画作为背景,意味着要取得动画,只要取得该View的背景即可,当然可以在代码中通过设置背景的方式指定;
apk:onClick指示按钮的动作,当然可以在代码中通过实现OnClickListener的方式实现。
4、Activity代码:
/** * Copyright (c) 2004-2011 All Rights Reserved. */ package com.aboy.android.study.animation; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import com.aboy.android.study.R; /** * Frame动画 * * @author obullxl@gmail.com * @version $Id: FrameActivity.java, v 0.1 2011-6-10 下午12:49:46 oldbulla Exp $ */ public class FrameActivity extends Activity { public static final String TAG = "FrameActivity" ; // 显示动画的组件 private ImageView imgDance; // Frame动画 private AnimationDrawable animDance; /** * @see android.app.Activity#onCreate(android.os.Bundle) */ public void onCreate(Bundle cycle) { super .onCreate(cycle); super .setContentView(R.layout.dance); // 实例化组件 this .imgDance = (ImageView) super .findViewById(R.id.ImgDance); // 获得背景(6个图片形成的动画) this .animDance = (AnimationDrawable) this .imgDance.getBackground(); } /** * 按钮:开始‘跳舞’动画 */ public void onStartDance(View view) { this .animDance.start(); } /** * 按钮:停止‘跳舞’动画 */ public void onStopDance(View view) { this .animDance.stop(); } } |
代码就那么的几行,运行之后,点击“开始”按钮后,动画一直不停的播放,直到点击“停止”为止。
本文作者:奔跑的蜗牛,转载请注明原文链接:https://ntopic.cn
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)