本文目录

  • 帧动画概念
  • 帧动画实现
    • 方法1:xml实现帧动画
      • 第一步:导入帧动画素材
      • 第二步:创建帧动画文件
      • 第三步:布局文件和Activity
    • 方法2:用Java代码实现帧动画

推荐阅读:
Android动画之补间动画用法最全详解
Android 属性动画:一文让你彻底了解和掌握属性动画用法

帧动画概念

在Android中,帧动画的本质是把一组预先准备好的图片循环切换播放,造成一种动画效果。

帧动画实现

实现帧动画有两种方式,即xmljava

方法1:xml实现帧动画

第一步:导入帧动画素材

把准备的素材放到drawable目录

第二步:创建帧动画文件

drawable目录下创建一个animation_flower.xml的文件,往文件中添加

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false">// 是否只播放一次 false 循环播放    <item        android:drawable="@drawable/img01"        android:duration="200" />    <item        android:drawable="@drawable/img02"        android:duration="200" />    <item        android:drawable="@drawable/img03"        android:duration="200" />    <item        android:drawable="@drawable/img04"        android:duration="200" />    <item        android:drawable="@drawable/img05"        android:duration="200" /></animation-list>

第三步:布局文件和Activity

布局文件:布局文件中添加两个点击按钮和一个图片控件

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"    tools:context=".MainActivity">    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="100dp"        android:text="开始动画"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="16dp"        android:text="结束动画"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/button" />    <ImageView        android:id="@+id/image"        android:layout_width="200dp"        android:layout_height="200dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        android:background="@drawable/animation_flower"        app:layout_constraintTop_toBottomOf="@+id/button2" /></androidx.constraintlayout.widget.ConstraintLayout>

Activity中添加启动&停止动画代码

public class MainActivity extends AppCompatActivity {    Button mButtonStart;    Button mButtonStop;    ImageView mImageViewShow;    AnimationDrawable mAnimationDrawable;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mImageViewShow = findViewById(R.id.image);        // 获取动画对象        mAnimationDrawable = (AnimationDrawable) mImageViewShow.getBackground();        mButtonStart = findViewById(R.id.button);        mButtonStart.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //开始动画                mAnimationDrawable.start();            }        });        mButtonStop = findViewById(R.id.button2);        mButtonStop.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //结束动画                mAnimationDrawable.stop();            }        });    }}

方法2:用Java代码实现帧动画

Java代码实现帧动画跟xml很类似,只有Activity部分有点区别

public class MainActivity extends AppCompatActivity {    Button mButtonStart;    Button mButtonStop;    ImageView mImageViewShow;    AnimationDrawable mAnimationDrawable;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mImageViewShow = findViewById(R.id.image);        // 获取动画对象        mAnimationDrawable =new AnimationDrawable();        mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img01),200);        mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img02),200);        mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img03),200);        mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img04),200);        mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img05),200);        mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.img06),200);        mAnimationDrawable.setOneShot(false);//设置循环播放        mImageViewShow.setBackground(mAnimationDrawable);        mButtonStart = findViewById(R.id.button);        mButtonStart.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //开始动画                mAnimationDrawable.start();            }        });        mButtonStop = findViewById(R.id.button2);        mButtonStop.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //结束动画                mAnimationDrawable.stop();            }        });    }}

作者:lucashu
出处:https://blog.csdn.net/huweiliyi/article/details/105669298
原创不易,欢迎转载,但未经作者同意请保留此段声明,并在文章页面明显位置给出原文链接。

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. NPM 和webpack 的基础使用
  3. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  4. python list.sort()根据多个关键字排序的方法实现
  5. 读取android手机流量信息
  6. Android(安卓)Activity界面切换添加动画特效
  7. android EditText设置不可写
  8. Android(安卓)拨号器的简单实现
  9. android 使用html5作布局文件: webview跟javascript交互

随机推荐

  1. Android、js&html5的基础资料&书籍
  2. 刚进入Android终端即可使用busybox的命令
  3. pc 应用 通过 usb adb 与 android客户端
  4. 第1章 Android图像概述
  5. 我也分享一下我Android的收入数据
  6. Android 6.x 新的运行时权限
  7. Android 开发之关于 drawable 你必须知道
  8. Android 文件保存getFilesDir()丶getCach
  9. Android -中毒
  10. Android 事件处理详解(一) —— 基于监听的