动画系列:RecyclerView中,AnimationDrawable.stop()动画没有停止。

  • Android动画分为三类:

    1. 逐帧动画 AnimationDrawable

    2. 补间动画

      • 平移动画 TranslateAnimation

      • 缩放动画 ScaleAnimation

      • 旋转动画 RotateAnimation

      • 透明度动画 AlphaAnimation

    3. 属性动画

  • 今天在做一个逐帧动画时,遇到一个bug:AnimationDrawable的stop()方法调用了,动画并没有停止。写了个小Demo,效果如下:

bug在demo中没有复现,发现只在RecyclerView中适配器里面,才会出问题。解决方案是停止播放的代码需要加两行代码,如下:

 animationDrawable.stop();
 animationDrawable.selectDrawable(0);
 ivPlaySound.setImageDrawable(null);
 ivPlaySound.setImageResource(R.drawable.animate_sound_play);

完整Activity代码:

public class MainActivity extends AppCompatActivity {
    ImageView ivPlaySound;
    private AnimationDrawable animationDrawable;

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

         ivPlaySound = findViewById(R.id.iv_play_sound);
    }

    public void startAnim(View view) {
        Log.i("xx", "开始");
        animationDrawable = (AnimationDrawable) ivPlaySound.getDrawable();
        animationDrawable.start();
    }

    public void stopAnim(View view) {
        Log.i("xx", "结束");
        animationDrawable.stop();
        animationDrawable.selectDrawable(0);
// RecyclerView中停止,需要加下面这两行代码
//        ivPlaySound.setImageDrawable(null);
//        ivPlaySound.setImageResource(R.drawable.animate_sound_play);
    }
}

布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#FFD86A"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="5dp">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:text="宝贝发音"
            android:textColor="#ff8d6d44"
            android:textSize="10sp" />
        <ImageView
            android:id="@+id/iv_play_sound"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:src="@drawable/animate_sound_play" />

    </LinearLayout>

    <Button
        android:id="@+id/btn1"
        android:layout_below="@id/ll"
        android:layout_marginLeft="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startAnim"
        android:text="开始播放"/>

    <Button
        android:layout_below="@id/ll"
        android:layout_toRightOf="@id/btn1"
        android:layout_marginLeft="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结束播放"
        android:onClick="stopAnim"
        />

</RelativeLayout>

动画文件animation_sound_play.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/sound1"
        android:duration="200" />
    <item
        android:drawable="@drawable/sound2"
        android:duration="200" />
    <item
        android:drawable="@drawable/sound3"
        android:duration="200" />
    <item
        android:drawable="@drawable/sound4"
        android:duration="200" />
</animation-list>

图片也一起上传吧!一次是sound1、sound2、sound3、sound4,为了看的明显加了背景色,可以右键下载。

猜你喜欢

转载自blog.csdn.net/zhangjin1120/article/details/114939762