Farmers in yards world, beautiful application experience, from the programmer for the processing of detail, and self requirements state, agriculture in the yard a member of the young people is busy, every day, every week, can leave some footprints, is the creation of content, there is a persistent, is I don’t know why, if you lost, might as well to Chou Chou code track of farmers.

If you are interested, you can follow the public account Biglead to get the latest learning materials.

When a group of pictures with similar actions are combined together and then played for a certain period of time, an animation will be formed, which we can call frame animation.

This can be easily implemented in Android by combining XML.

1 Create an XML animation file

Create an animation file in the Drawable directory of your Android project, such as the one I created here load.xml


      
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
>
    <item
        android:drawable="@mipmap/ani_1"
        android:duration="800" />
    <item
        android:drawable="@mipmap/ani_2"
        android:duration="800" />
    <item
        android:drawable="@mipmap/ani_3"
        android:duration="800" />
    <item
        android:drawable="@mipmap/ani_4"
        android:duration="800" />
    <item
        android:drawable="@mipmap/ani_5"
        android:duration="800" />
    <item
        android:drawable="@mipmap/ani_6"
        android:duration="800" />
    <item
        android:drawable="@mipmap/ani_7"
        android:duration="800" />
    <item
        android:drawable="@mipmap/ani_8"
        android:duration="800" />
</animation-list>
Copy the code

Set the Android :oneshot property to true, which means that the animation is executed only once and stops at the last frame. Set to false to play in a loop. Files can be added as Image backgrounds and played when triggered.

2 Loading usage

In your Activity, use an ImageView to load and display the animation file directly as follows:

public class MainActivity extends AppCompatActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ImageView lImageView = findViewById(R.id.imageview);
		// Set the image
		lImageView.setImageResource(R.drawable.loading);
		/ / moveAnimationDrawable lAnimationDrawable = (AnimationDrawable) lImageView.getDrawable(); lAnimationDrawable.start(); }}Copy the code

Talk about AnimationDrawable

Drawable Animation Can load a Drawable resource to animate frames. AnimationDrawable is the basic class for implementing Drawable animations

Sometimes the AnimationDrawable. Start method is called to start the animation, but nothing happens.

3.1 use ViewTreeObserver

ViewTreeObserver.OnPreDrawListener lOnPreDrawListener=new ViewTreeObserver.OnPreDrawListener(){
	@Override
	public boolean onPreDraw(a) {
		lAnimationDrawable.start();
		return true; // Notice the value returned by this line}};// Bind the view observer
lImageView.getViewTreeObserver().addOnPreDrawListener(lOnPreDrawListener);
Copy the code

3.2 use RunAnim

public class MainActivity extends AppCompatActivity {
	

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

	private AnimationDrawable mAnimationDrawable;
	
	public void start3(a) {
		ImageView lImageView = findViewById(R.id.imageview);
		// Set the image
		lImageView.setImageResource(R.drawable.loading);
		/ / move
		mAnimationDrawable = (AnimationDrawable) lImageView.getDrawable();
		
		RunAnim runAnim = new RunAnim();
		runAnim.execute("");
		
		
	}
	
	class RunAnim extends AsyncTask<String.String.String> {
		@Override
		protected String doInBackground(String... params) {
			if(! mAnimationDrawable.isRunning()) { mAnimationDrawable.stop(); mAnimationDrawable.start(); }return ""; }}}Copy the code

3.3 Called in onWindowFocusChanged of the Activity

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
	mAnimationDrawable.start();
	super.onWindowFocusChanged(hasFocus);
}
Copy the code

The completion of

The source code is here