Android frame animation invalid solution

Record the inexplicable invalid problem when applying frame animation today. After some searching and solving, it is finally solved.
The first is the Drawable file of the frame animation. Note: The image resource file must be placed in the res/Drawable directory

<?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/image1_1" android:duration="1000"/>
    <item android:drawable="@drawable/image1_2" android:duration="1000"/>

</animation-list>

Next, ImageView sets the background property

 <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/anima_jiaojing" />

Usually, this can achieve the animation effect, but today there is no animation effect inexplicably, once I searched it and found that you
need to add these two lines of code, and these two lines of code must be added in onWindowFocusChanged

	@Override
    public void onWindowFocusChanged(boolean hasFocus) {
    
    
        super.onWindowFocusChanged(hasFocus);
        //解决帧动画没效果、不能在onCreate方法中调用,由于onCreate时Drawable未完成绘制
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
    }

Guess you like

Origin blog.csdn.net/qq_44720366/article/details/107255534