Android帧动画无效解决方案

记录一下今天应用帧动画的时候莫名无效的问题,经过一番查找解决,终于是搞定了。
首先是帧动画的Drawable文件,注意:这里的图片资源文件必须放在res/Drawable目录下

<?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>

紧接着,ImageView设置好background属性

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

往常,这样就能实现动画效果了,但是今天莫名其妙没有动画效果,一经查找发现
需要加上这么两行些代码,并且这两行代码必须加在onWindowFocusChanged里面

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

猜你喜欢

转载自blog.csdn.net/qq_44720366/article/details/107255534
今日推荐