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"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    
    
    

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            android:drawable="@mipmap/img24"
        android:duration="50" />

3、在activity_main.xml中添加控件

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">

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


    

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);
}




你可能感兴趣的:(Android逐帧动画)