android - 在 if 语句中只激活一次动画
问题描述
我有一个倒计时计时器,我使用了一些 if 语句来标记倒计时中的某些间隔。我想播放一个动画,我将 TextView 从一个点移动到另一个点,但我希望它在间隔内完成一次。例如,我设置了一个从 0 到 60000 毫秒的时间间隔,我希望动画在该时间间隔内播放一次,并带有以下动画:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="0%"
android:toYDelta="-50%"
android:duration="800" />
</set>
我的计时器代码的一部分:
private void startTimer() {
Log.println(Log.ASSERT, "CHECK","Entered startTimer() method");
millisInFuture = mTimeLeftInMillis;
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
final long millisPassed = millisInFuture - mTimeLeftInMillis;
progress = (int) (millisPassed * 100 / millisInFuture);
pb.setProgress(progress);
pb2.setProgress(0);
pb3.setProgress(0);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Key: 60 sec
if (millisInFuture == 480000) {
if (millisPassed <= 60000 || (millisPassed > 180000 && millisPassed <= 240000) || (millisPassed > 300000 && millisPassed <= 360000 || (millisPassed > 420000 && millisPassed <= 480000))) {
// Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.fade_in);
// stepupimage.setAnimation(animation);
setflowrate();
Log.println(Log.ASSERT,"CHECK","Check that the first if statement of key 60 is entered");
statusIfUp();
time_of_stage = (millisInFuture - millisPassed) % 60000;
progress2 = (int) (time_of_stage*100 / 60000);
Log.println(Log.VERBOSE,"CHECK","TIME OF STAGE = "+time_of_stage);
Log.println(Log.VERBOSE,"CHECK","progress2= "+progress2);
pb2.setProgress(progress2);
updateStageUpCount();
upArrowAnimation();
setflowrate是我用来调用动画的方法
private void setflowrate()
{
flow.setText(String.valueOf(num) + " GPM");
Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.move_up);
flow.setAnimation(animation);
linearlayoutofflow.setAnimation(animation);
progressBar.setAnimation(animation);
}
flow、linearlayoutflow、progressbar 应该在上面的时间间隔内向上移动,但我希望每个间隔只发生一次移动。
编辑:问题是动画一直在循环。我希望它在每秒调用一次的 onTick 方法中发生一次。我使用 if 语句指定的时间间隔我希望动画只在其中发生一次。
解决方案
我想到的最简单的方法是设置一个布尔标志,如果为真则播放动画并将布尔值设置为假,如果为假则不播放动画。
private boolean animate = true;
private void setflowrate() {
if(animate) {
flow.setText(String.valueOf(num) + " GPM");
Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.move_up);
flow.setAnimation(animation);
linearlayoutofflow.setAnimation(animation);
progressBar.setAnimation(animation);
animate = false;
}
}
在所有进程结束时,或者正如您所说的“间隔”,您再次将标志设置为 true,以防您希望能够重新启动动画。
推荐阅读
- c# - 如何公开 C# .net 核心 Web Api 端点以供其他应用程序调用?
- powerbi - 在 Power BI 中筛选表行
- javascript - 如何从 Obj 加载下拉菜单
- spring-boot - 如何将以下 mongo 集合构建到 redis 缓存中?
- node.js - 错误:无法启动浏览器进程!傀儡师
- html - 无法在html中获取多个复选框的值
- python-3.x - Crontab 没有运行我的脚本(MacOS Catalina 10.15.5)
- c# - C# 使用 ssh.net 从 SSH 服务器检索数据时出错
- reactjs - reactnavigation v5 中的 NavigationActions 替代方案是什么?
- logstash - 通过 http 发送 filebeat 输出