【问题标题】:make lottie animation setVisibility immediately立即制作 lottie 动画 setVisibility
【发布时间】:2020-08-26 22:41:26
【问题描述】:

我有一个 adroid 项目,我想在算法运行期间显示一个 lottie 动画,但它有时不可见。

在我的 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 时,如何让 lottie 立即可见? 谢谢

【问题讨论】:

  • 试试 lottieanimationview.setVisibilty(View.VISIBLE);
  • @AkashBisht 我试过了,但在函数运行结束时仍然出现了彩票。
  • 您可以尝试将乐透动画视图包裹在一个布局中,例如线性布局,然后尝试设置布局的可见性。
  • @AkashBisht 我是 android studio 的新手,请问您可以扩展我应该怎么做吗?谢谢

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


【解决方案1】:

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

我有一些乐天观点(下面附上截图)。 但我们将只使用切换视图和 thumbsDown 当我们打开 Toggle 时,我们正在设置 thumbsdown 视图可见性消失 当我们关闭 Toggle 时,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" />

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

主要是:

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    相关资源
    最近更新 更多