Android逐帧动画

逐帧动画简单介绍:

逐帧动画【Frame Animation】,即顺序播放事先做好的图像,跟电影类似 ,也叫Drawable Animation动画,是最简单最直观动画类型。有两种实现方式。

逐帧动画XML资源文件方式

这个是最常用的方式,在res/drawable目录下新建动画XML文件,如下所示 

一、在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="false">

    <!-- animation-list 帧动画 -->
    <!-- android:oneshot的值为 false代表播放多次,true代表只播放一次 -->
    <!-- duration代表每张图片的播放时间 ,定义一个持续时间为50毫秒的动画帧 -->

    <item
        android:drawable="@mipmap/img00"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img01"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img02"
        android:duration="50" />
    <item
        android:drawable="@mipmap/img03"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img04"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img05"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img06"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img07"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img08"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img09"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img10"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img11"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img12"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img13"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img14"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img15"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img16"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img17"
        android:duration="50" />
    <item
        android:drawable="@mipmap/img18"
        android:duration="50" />
    <item
        android:drawable="@mipmap/img19"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img20"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img21"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img22"
        android:duration="50" />
    <item
        android:drawable="@mipmap/img23"
        android:duration="50" />

    <item
        android:drawable="@mipmap/img24"
        android:duration="50" />
</animation-list>

3、在activity_main.xml中添加控件

<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="com.startimes.animation.MainActivity">

    <ImageView
        android:id="@+id/mImageView"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp" />


    <Button
        android:id="@+id/startButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start" />

    <Button
        android:id="@+id/stopButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop" />
</LinearLayout>

4、在代码中获取并开启帧动画

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mImageView;
    private Button startButton;
    private Button stopButton;
    private AnimationDrawable animationDrawable;

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

        mImageView = (ImageView) findViewById(R.id.mImageView);
        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);

        startButton.setOnClickListener(this);
        stopButton.setOnClickListener(this);
        XMLStartFrameAnimation();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.startButton:
                if (animationDrawable!=null){
                    animationDrawable.start();
                }
                break;

            case R.id.stopButton:
                if (animationDrawable!=null&&animationDrawable.isRunning()){
                    animationDrawable.stop();
                }
                break;

            default:
                break;
        }
    }


    public void XMLStartFrameAnimation(){
        animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation_list);
        mImageView.setBackground(animationDrawable);
    }
}

二、通过代码实现帧动画

public void startCodeFrameAnimation(){
    animationDrawable = new AnimationDrawable();
    // 为AnimationDrawable添加动画帧
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img00), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img01), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img02), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img03), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img04), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img05), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img06), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img07), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img08), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img09), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img10), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img11), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img12), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img13), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img14), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img15), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img16), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img17), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img18), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img19), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img20), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img21), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img22), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img23), 50);
    animationDrawable.addFrame(
            getResources().getDrawable(R.mipmap.img24), 50);
    // 设置为循环播放
    animationDrawable.setOneShot(false);
    mImageView.setBackground(animationDrawable);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值