如何在同一个 activity 上播放 gif 几秒钟

How to splash gif on the same activity for few seconds

游戏结束时,我想在同一 activity 中显示背景淡出的 GIF 图片。

我尝试使用 GIFImageView 创建一个新的闪屏。然后在我的“游戏结束”方法中像这样启动 activity

Intent intent = new Intent(getApplicationContext(), SplashActivity.class);
startActivity(intent);

但是,这不是我想要的。在“gameover”方法中创建了一个白色背景的新 activity。如何在相同的 activity 中显示背景褪色的 GIF?

这是我的 SplashActivity:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".SplashActivity">

    <pl.droidsonroids.gif.GifImageView
        android:layout_width="407dp"
        android:layout_height="559dp"
        android:src="@drawable/fireworks"
        android:alpha="0.5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity 的 java 代码在这里:

public class SplashActivity extends AppCompatActivity {

    private final int SPLASH_DISPLAY_LENGTH = 8000;

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

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                
                Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}

但是,是分开的activity。我想将我的 gif 作为同一个主 activity 的一部分。只需在调用“gameover”方法时使其可见即可。

我已经解决了这个问题,方法是将 gif 图像视图移动到主视图 activity

<pl.droidsonroids.gif.GifImageView
        android:id="@+id/fireworks_gif"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.75"
        android:src="@drawable/fireworks"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

然后在“gameover”方法之后

GifImageView fireworks = findViewById(R.id.fireworks_gif);
            fireworks.setVisibility(GifImageView.VISIBLE);
            imageView.setVisibility(View.INVISIBLE);
            layout.setVisibility(View.INVISIBLE);
            new Handler().postDelayed(new Runnable(){
                @Override
                public void run() {
                    /* Create an Intent that will start the Main-Activity. */
                    Intent mainIntent = new Intent(this,MainActivity.class);
                    this.finish();
                }
            }, 10000);

因此,gif 视图存在 10 秒,然后自动重定向到 MainActivity。