android set-frame animation
Display effects:
1. Layout Interface: setting layout file layout format layout LinearLayout linear, vertical direction.
2. Create an animation resource file: Create a resource file using animation-list fairy.xml animate resources in the resource file inside the drawable below res following:
DURATION = "60": each frame residence time of 60 ms
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/img1" android:duration="60"/>
<item android:drawable="@drawable/img1" android:duration="60"/>
<item android:drawable="@drawable/img2" android:duration="60"/>
<item android:drawable="@drawable/img3" android:duration="60"/>
<item android:drawable="@drawable/img4" android:duration="60"/>
<item android:drawable="@drawable/img5" android:duration="60"/>
<item android:drawable="@drawable/img6" android:duration="60"/>
</animation-list>
3. By java control the animation start and pause, record player with a Boolean variable state, AnimationDrawable get animation resource file, and then set the monitor events on the layout document prepared if ... else calls start in which () and stop () method to start stop the animation.
package com.example.acer.donghua;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private boolean flag=true; //记录播放状态
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout=findViewById(R.id.layout1);//获取布局管理器
final AnimationDrawable anim=(AnimationDrawable)linearLayout.getBackground();//获取动画资源文件
//为布局管理器添加监听事件
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag){
anim.start(); //播放功能
flag=false;
}else {
anim.stop(); //停止播放
flag=true;
}
}
});
}
}