我有一个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文件中)。
现在,不是先看到然后再运行另一个函数(我想让它像一个加载动画一样),而是让Lottie变得可见。之后 如何才能在调用setVisibility的时候,让lottie立即变得可见呢?
好的... 这是我试过的代码。
我有一些lottie视图(截图附在下面).但我们将只使用toggle视图和thumbsDown当我们打开Toggle时,我们设置thumbsdown视图visibilty消失了,而当我们关闭Toggle时,thumbsdown视图又是可见的。
活动_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
public class MainActivity extends 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);
希望对你有所帮助。