首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Android动画之帧动画

Android动画之帧动画

作者头像
计蒙不吃鱼
发布2025-06-12 14:27:29
发布2025-06-12 14:27:29
18500
代码可运行
举报
文章被收录于专栏:Android开发Android开发
运行总次数:0
代码可运行

帧动画的原理就是让一系列的静态图片依次播放,实现动画效果。

下面了解一下两种实现帧动画的方式

1.利用 Java 代码实现帧动画

2.利用 xml 实现帧动画(开发中通常使用这种方法实现帧动画)

1.利用 Java 代码实现帧动画

源代码如下:

activity_main.xml

代码语言:javascript
代码运行次数:0
运行
复制
<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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启" />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止" />

    <ImageView
        android:id="@+id/iv_animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java文件如下

代码语言:javascript
代码运行次数:0
运行
复制
public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    AnimationDrawable animationDrawable;
    private Button btn_start;
    private Button btn_stop;
    private ImageView iv_animation;//java的形式实现


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    private void initView() {
        btn_start = (Button) findViewById(R.id.btn_start);
        btn_stop = (Button) findViewById(R.id.btn_stop);
        iv_animation = (ImageView) findViewById(R.id.iv_animation);

        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                //1.实现帧动画的类(相当于一本空白的小人书)
                animationDrawable = new AnimationDrawable();
                //2.为帧动画添加内容(在小人书里添加内容)
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim1)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim2)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim3)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim4)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim5)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim6)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim7)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim8)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim9)), 100);
                animationDrawable.addFrame(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.anim10)), 100);
                //3.将帧动画设置给View做背景
                iv_animation.setBackground(animationDrawable);
                //其他操作,如,设置只执行一次
                animationDrawable.setOneShot(true);
                //4.开启动画(相当于翻书)
                animationDrawable.start();
                break;
            case R.id.btn_stop:
                //停止动画
                animationDrawable.stop();
                break;
        }
    }
}

2.利用 xml 实现帧动画(开发中通常使用这种方法实现帧动画)

以前还在学校的时候写过一个例子Android中帧动画的简单实现

下面再来一次

(1).帧动画通常在XML 资源进行定义,在 <animation-list …/> 标签下使用 <item …/> 子元素标签定义动画的全部帧,并指定各帧的持续时间。

2、布局中将 AnimationDrawable 对象直接作为背景

3.在Activity文件中进行操作(播放,停止)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-08-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档