19. 帧动画
把几张图片进行快速播放形成的动画。
19.1 素材准备
8张图
19.2 创建animation-list集合
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/frame1" android:duration="120"/>
<item android:drawable="@drawable/frame2" android:duration="120"/>
<item android:drawable="@drawable/frame3" android:duration="120"/>
<item android:drawable="@drawable/frame4" android:duration="120"/>
<item android:drawable="@drawable/frame5" android:duration="120"/>
<item android:drawable="@drawable/frame6" android:duration="120"/>
<item android:drawable="@drawable/frame7" android:duration="120"/>
<item android:drawable="@drawable/frame8" android:duration="120"/>
</animation-list>
19.3 编写布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/frame"/>
19.4 动画的启动和停止
package com.dingjiaxiong.myzhuzhendonghua;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
//标志位
private boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = findViewById(R.id.rl);
AnimationDrawable anim = (AnimationDrawable) relativeLayout.getBackground();
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag){
anim.start();
flag = false;
}else{
anim.stop();
flag = true;
}
}
});
}
}
运行