Android中实现一个简单的逐帧动画(附代码下载)

Android中实现一个简单的逐帧动画(附代码下载),第1张

概述场景Android中的逐帧动画,就是由连续的一张张照片组成的动画。效果  注:博客:https://blog.csdn.net/badao_liumang_qizhi关注公众号霸道的程序猿获取编程相关电子书、教程推送与免费下载。实现首先准备一组不同表情的照片,放在res/drawable下,然后在此目录下新建动画 场景

AndroID中的逐帧动画,就是由连续的一张张照片组成的动画。

效果

 

 

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

首先准备一组不同表情的照片,放在res/drawable下,然后在此目录下新建动画资源文件fairy.xml

<?xml version="1.0" enCoding="utf-8"?><animation-List xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item androID:drawable="@drawable/img001" androID:duration="60"/>    <item androID:drawable="@drawable/img002" androID:duration="60"/>    <item androID:drawable="@drawable/img003" androID:duration="60"/>    <item androID:drawable="@drawable/img004" androID:duration="60"/>    <item androID:drawable="@drawable/img005" androID:duration="60"/>    <item androID:drawable="@drawable/img006" androID:duration="60"/></animation-List>

 

这里是逐帧动画,所以节点是animation-List 。

然后来到布局文件,将布局设置为linearLayout并添加ID属性,并且设置背景为上面添加的动画资源文件

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:ID="@+ID/linearLayout"    androID:orIEntation="vertical"    androID:background="@drawable/fairy"    androID:layout_height="match_parent"    tools:context=".MainActivity"> </linearLayout>

 

然后来到对应的Activity,创建标识变量Flag,然后获取AnimationDrawable对象,并且为布局管理器添加单击事件。从而控制动画的停止和播放。

package com.badao.animationtest;import androIDx.appcompat.app.AppCompatActivity;import androID.graphics.drawable.AnimationDrawable;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.linearLayout;public class MainActivity extends AppCompatActivity {    private boolean flag = true;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        linearLayout linearLayout= (linearLayout) findVIEwByID(R.ID.linearLayout); //获取布局管理器        //获取AnimationDrawable对象        final AnimationDrawable anim= (AnimationDrawable) linearLayout.getBackground();        linearLayout.setonClickListener(new VIEw.OnClickListener() {  //为布局管理器添加单击事件            @OverrIDe            public voID onClick(VIEw v) {                if(flag){                    anim.start(); //开始播放动画                    flag=false;                }else {                    anim.stop();  //停止播放动画                    flag=true;                }            }        });    }}

 

代码下载

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12097211

总结

以上是内存溢出为你收集整理的Android中实现一个简单的逐帧动画(附代码下载)全部内容,希望文章能够帮你解决Android中实现一个简单的逐帧动画(附代码下载)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067291.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存