立即制作抽奖动画setVisibility

时间:2020-05-11 11:40:30

标签: android android-studio animation android-animation lottie

我有一个adroid项目,我想在算法运行期间显示很多动画,但有时却看不到。

在我的xml文件中:

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/animation_view"
    android:layout_width="100dp"
    android:layout_height="100dp"

    android:layout_marginLeft="200dp"
    android:layout_marginTop="250dp"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    android:visibility="gone" 
    app:lottie_rawRes="@raw/loading" />

//我也尝试了android:visibility =“ invisible”

在Java中:

LottieAnimationView lottieanimationview;
lottieanimationview = findViewById(R.id.animation_view);

在我要开始看动画的特定功能中,我这样写:

lottieanimationview.setVisibility(LottieAnimationView.VISIBLE);

然后,在此函数中,我调用了另一个运行该算法的函数(在项目中的cpp文件中)。

现在,而不是显示出来,然后运行另一个功能(我希望它像加载动画一样), 在第二个函数完成后的 之后,该彩票便可见。致电setVisibility时,如何使彩票立即可见? 谢谢

1 个答案:

答案 0 :(得分:0)

好..这是我尝试过的代码:

我对此有一些观点(下面的截图)。 但是我们将只使用切换视图和thumbsDown 当我们打开“切换”时,我们将“拇指向下”视图的可见性设置为“消失” 并且当我们关闭“切换”功能时,“拇指向下”视图再次可见。

activity_main.xml

<!-- Custom Action Bar -->
<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/lav_actionBar"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    android:layout_marginEnd="0dp"
    android:scaleType="centerCrop"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="true"
    app:lottie_fileName="gradient_bg.json"
    app:lottie_loop="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="Demo Lottie"
    android:textColor="@android:color/white"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="@+id/lav_actionBar"
    app:layout_constraintEnd_toEndOf="@+id/lav_actionBar"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/lav_actionBar" />

<!-- Thumbs Up Button -->
<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/lav_thumbUp"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginStart="80dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="false"
    app:lottie_fileName="thumb_up.json"
    app:lottie_loop="false"
    app:lottie_speed="1.25" />

<!-- Thumbs Down Button (We just rotate the previous one by 180 deg ;) )-->
<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/lav_thumbDown"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="80dp"
    android:layout_marginBottom="8dp"
    android:rotation="180"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:lottie_autoPlay="false"
    app:lottie_fileName="thumb_up.json"
    app:lottie_loop="false"
    app:lottie_speed="1.25" />

<!-- Toggle Switch -->
<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/lav_toggle"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/lav_thumbUp"
    app:layout_constraintVertical_bias="0.4"
    app:lottie_autoPlay="false"
    app:lottie_fileName="toggle_switch.json"
    app:lottie_loop="false"
    app:lottie_speed="1.75" />

enter image description here

MainActivity.java

公共类MainActivity扩展了AppCompatActivity {

LottieAnimationView thumb_up;
LottieAnimationView thumb_down;
LottieAnimationView toggle;
LottieAnimationView imprint;
int flag = 0;


@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    thumb_up = findViewById(R.id.lav_thumbUp);
    thumb_up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            thumb_down.setProgress(0);
            thumb_down.pauseAnimation();
            thumb_up.playAnimation();
            Toast.makeText(MainActivity.this, "Cheers!!", Toast.LENGTH_SHORT).show();
            //---- Your code here------
        }
    });

    thumb_down = findViewById(R.id.lav_thumbDown);
    thumb_down.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            thumb_up.setProgress(0);
            thumb_up.pauseAnimation();
            thumb_down.playAnimation();
            Toast.makeText(MainActivity.this, "Boo!!", Toast.LENGTH_SHORT).show();
            //---- Your code here------
        }
    });

    toggle = findViewById(R.id.lav_toggle);
    toggle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            changeState();
        }
    });
} //
private void changeState() {
    if (flag == 0) {
        toggle.setMinAndMaxProgress(0f, 0.43f);
        toggle.playAnimation();
        flag = 1;
        //---- Your code here------
        thumb_down.setVisibility(View.GONE);
    } else {
        toggle.setMinAndMaxProgress(0.5f, 1f);
        toggle.playAnimation();
        flag = 0;
        //---- Your code here------
        thumb_down.setVisibility(View.VISIBLE);
    }
} }

enter image description here

主要是:

thumb_down.setVisibility(View.GONE); 
thumb_down.setVisibility(View.VISIBLE);

希望有帮助。