Animation series: In RecyclerView, AnimationDrawable.stop() animation does not stop.

  • Android animation is divided into three categories:

    1. AnimationDrawable Frame by Frame

    2. Tween animation

      • Translate Animation

      • ScaleAnimation

      • Rotate Animation

      • AlphaAnimation

    3. Property animation

  • When I was doing a frame-by-frame animation today, I encountered a bug: the stop() method of AnimationDrawable was called, and the animation did not stop. Wrote a small Demo, the effect is as follows:

The bug did not reappear in the demo, and it was found that problems occurred only in the adapter in RecyclerView. The solution is to add two lines of code to the code to stop the playback, as follows:

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

Complete Activity code:

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);
    }
}

Layout file code:

<?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 file animation_sound_play.xml code:

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

Upload the pictures together! One time is sound1, sound2, sound3, and sound4. In order to see the background color clearly, you can right-click to download.

Guess you like

Origin blog.csdn.net/zhangjin1120/article/details/114939762