一:在drawable目录下新建just.xml文件和against.xml文件
顺序显示动画文件:just.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/img01" android:duration="400"/>
<item android:drawable="@drawable/img02" android:duration="400"/>
<item android:drawable="@drawable/img03" android:duration="400"/>
<item android:drawable="@drawable/img04" android:duration="400"/>
<item android:drawable="@drawable/img05" android:duration="400"/>
<item android:drawable="@drawable/img06" android:duration="400"/>
<item android:drawable="@drawable/img07" android:duration="400"/>
<item android:drawable="@drawable/img08" android:duration="400"/>
<item android:drawable="@drawable/img09" android:duration="400"/>
</animation-list>
倒序显示动画文件:against.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/img09" android:duration="400"/>
<item android:drawable="@drawable/img08" android:duration="400"/>
<item android:drawable="@drawable/img07" android:duration="400"/>
<item android:drawable="@drawable/img06" android:duration="400"/>
<item android:drawable="@drawable/img05" android:duration="400"/>
<item android:drawable="@drawable/img04" android:duration="400"/>
<item android:drawable="@drawable/img03" android:duration="400"/>
<item android:drawable="@drawable/img02" android:duration="400"/>
<item android:drawable="@drawable/img01" android:duration="400"/>
</animation-list>
二:布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Activity9"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="利用animation-list实现帧动画"
android:textSize="23sp"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:gravity="center"
android:layout_marginTop="60dp"
android:layout_marginBottom="80dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"/>
<Button
android:id="@+id/btn_just"
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="停止播放"/>
<Button
android:id="@+id/btn_against"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="倒序播放"/>
</LinearLayout>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
三:调用
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
public class Activity9 extends AppCompatActivity {
private AnimationDrawable animationDrawable;
private Button btn_just;
private Button btn_stop;
private Button btn_against;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_9);
initView();
}
private void initView() {
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
btn_just = findViewById(R.id.btn_just);
btn_stop = findViewById(R.id.btn_stop);
btn_against = findViewById(R.id.btn_against);
image = findViewById(R.id.image);
btn_just.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(R.drawable.just);
animationDrawable = (AnimationDrawable) image.getDrawable();
animationDrawable.start();
}
});
btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationDrawable = (AnimationDrawable) image.getDrawable();
animationDrawable.stop();
}
});
btn_against.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
image.setImageResource(R.drawable.against);
animationDrawable = (AnimationDrawable) image.getDrawable();
animationDrawable.start();
}
});
}
}
四:运行效果