【问题标题】:Start animation if passed 5 seconds如果超过 5 秒,则开始动画
【发布时间】:2015-08-05 08:15:16
【问题描述】:

我如何检查 Java (Android) 中是否已经过了 5 秒。

if (passedTime >= 5) {
    startAnimation();
} else {
    Log.d("TAG", passedTime);
}

【问题讨论】:

    标签: java android timer


    【解决方案1】:

    您可以为此使用System.nanoTime() 来测量经过的时间。

    例如,你可以这样做:

    long start = System.nanoTime();
    while (System.nanoTime() - start < 5000000);
    StartAnimation();
    

    或者,如果它不是主线程并且线程除了启动动画没有其他目的:

    try {
        Thread.sleep(5000);
        startAnimation();
    } catch (InterruptedException e) {
         // handle Interruption Exception here
    }
    

    另外,如果线程正在做一些其他的事情,你可以做(​​但不会精确到 5 秒);

    long start = System.nanoTime();
    bool doStuff = true;
    while (doStuff) {
        if (System.nanoTime() - start >= 5000000) {
            StartAnimation();
        }
        // Do other stuff here
    }
    

    你们中的一些人启动了一个自己的线程来启动动画(在我看来这可能是最好的解决方案):

    new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(5000);
                // Make sure animation will run on UI Thread!
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        StartAnimation();
                    }
                });
            } catch (InterruptedException e) {
                // Handle exception
            }
    
        }
    }).start();
    

    编辑:

    阅读您的最后一条评论后: 我会将这个逻辑传递给 startAnimation:

    // Class variable:
    long lastAnimationStart = 0;
    
    void StartAnimation() {
        if (lastAnimationStart = 0 || System.nanoTime() - lastAnimationStart >= 5000000) {
            lastAnimationStart = System.nanoTime();
            // Animation stuff here
        }
    }
    

    【讨论】:

      【解决方案2】:

      您应该使用 Handler 并向其发布延迟消息。像这样的:

      private static final int MSG_START_ANIMATION = 1337;
      private Handler mAnimationHandler = new Handler() {
      
          @Override
          public void handleMessage(Message msg) {
              switch (msg.what) {
                  case MSG_START_ANIMATION:
                      startAnimation();
                      break;
              }
          }
      };
      private final ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
      
          }
      
          @Override
          public void onPageSelected(int position) {
              mAnimationHandler.removeMessages(MSG_START_ANIMATION);
              mAnimationHandler.sendEmptyMessageDelayed(MSG_START_ANIMATION, TimeUnit.SECONDS.toMillis(5));
          }
      
          @Override
          public void onPageScrollStateChanged(int state) {
      
          }
      };
      

      另外,阅读这篇关于通过内部 Handler 类泄漏上下文的博文:Android Design Patterns

      【讨论】:

        猜你喜欢
        • 2021-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-27
        • 2013-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多