Android动画之逐帧动画(FrameAnimation)详解
今天我们就来学习逐帧动画,废话少说直接上效果图如下:
帧动画的实现方式有两种:
一、在res/drawable文件夹下新建animation-list的XML实现帧动画
1、首先在res/drawable文件夹下添加img00-img24共25张图片
2、新建frame_anim.xml
- <?xml version="1.0" encoding="utf-8"?>
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true" >
- <!-- animation-list 帧动画 -->
- <!-- android:oneshot的值为 false代表播放多次,true代表只播放一次 -->
- <!-- duration代表每张图片的播放时间 ,定义一个持续时间为50毫秒的动画帧 -->
- <item
- android:drawable="@drawable/img00"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img01"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img02"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img03"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img04"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img05"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img06"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img07"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img08"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img09"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img10"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img11"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img12"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img13"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img14"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img15"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img16"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img17"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img18"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img19"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img20"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img21"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img22"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img23"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img24"
- android:duration="50"/>
- </animation-list>
3、在activity_main中添加控件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.havorld.frameanimation.MainActivity" >
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true" />
- <!-- android:background="@drawable/frame_anim" -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:orientation="horizontal"
- android:padding="10dp" >
- <Button
- android:id="@+id/start"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="播放" />
- <Button
- android:id="@+id/stop"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="停止" />
- </LinearLayout>
- </RelativeLayout>
4、在代码中获取并开启帧动画
- public class MainActivity extends Activity implements OnClickListener {
- private ImageView imageView;
- private AnimationDrawable animationDrawable;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = (ImageView) findViewById(R.id.imageView);
- findViewById(R.id.start).setOnClickListener(this);
- findViewById(R.id.stop).setOnClickListener(this);
- setXml2FrameAnim1();
- // setXml2FrameAnim2();
- }
- /**
- * 通过XML添加帧动画方法一
- */
- private void setXml2FrameAnim1() {
- // 把动画资源设置为imageView的背景,也可直接在XML里面设置
- imageView.setBackgroundResource(R.drawable.frame_anim);
- animationDrawable = (AnimationDrawable) imageView.getBackground();
- }
- /**
- * 通过XML添加帧动画方法二
- */
- private void setXml2FrameAnim2() {
- // 通过逐帧动画的资源文件获得AnimationDrawable示例
- animationDrawable = (AnimationDrawable) getResources().getDrawable(
- R.drawable.frame_anim);
- imageView.setBackground(animationDrawable);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.start:
- if (animationDrawable != null && !animationDrawable.isRunning()) {
- animationDrawable.start();
- }
- break;
- case R.id.stop:
- if (animationDrawable != null && animationDrawable.isRunning()) {
- animationDrawable.stop();
- }
- break;
- default:
- break;
- }
- }
- }
二、通过代码实现帧动画
- /**
- * 通过代码添加帧动画方法
- */
- private void setSrc2FrameAnim() {
- animationDrawable = new AnimationDrawable();
- // 为AnimationDrawable添加动画帧
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img00), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img01), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img02), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img03), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img04), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img05), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img06), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img07), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img08), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img09), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img10), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img11), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img12), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img13), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img14), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img15), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img16), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img17), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img18), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img19), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img20), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img21), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img22), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img23), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img24), 50);
- // 设置为循环播放
- animationDrawable.setOneShot(false);
- imageView.setBackground(animationDrawable);
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库