if-else 语句中的 GIF

GIF in if-else statement

我试图在下一个 activity 按下按钮时显示 gif

这是我的代码:

if (bringLighter == true){
     prologueOutcome.setText("You chose to bring the lighter.");
     prologueBackground.setBackgroundColor(Color.parseColor("#000000"));
     prologueOutcome.setTextColor(Color.WHITE);
} else {
    prologueOutcome.setText("You chose to not bring the lighter.");
}

我不知道如何将 gif 放入 true 条件下,我尝试将它放在 xml 中用于 activity,但 gif 在两种条件下都显示。

XML代码:

<pl.droidsonroids.gif.GifImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/lighter"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

我想你可以根据条件更改打火机 GifImageViewvisibility,参考 Java 中的视图并检查条件

findViewById(R.id.lighter).setVisibility(bringLighter ? View.VISIBLE else View.GONE)

您可以通过两种方式完成。

1- 在 RelativeLayout 中添加图像。 给布局一个 Id。 在代码中调用布局和图像。 从布局中任意位置删除图像。

 <RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

        <pl.droidsonroids.gif.GifImageView
           android:id="@+id/gif_image"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/lighter"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

JAVA:

  View view = inflater.inflate(R.layout.your_layout, container, false);
RelativeLayout relativeLayout = view.findViewById(R.id.relativeLayout);
pl.droidsonroids.gif.GifImageView gif_image = view.findViewById(R.id.gif_image);

 relativeLayout.removeView(gif_image);

2- 从 XML 中删除 part:android:src="@drawable/lighter"; 仅在 JAVA 代码中添加图像。

   View view = inflater.inflate(R.layout.your_layout, container, false);
    pl.droidsonroids.gif.GifImageView gif_image =  view.findViewById(R.id.gif_image);



   imageView[ID].setBackgroundResource(R.drawable.lighter);