使用handler.postDelayed时获取剩余时间 [英] Get remaining time when using handler.postDelayed

查看:487
本文介绍了使用handler.postDelayed时获取剩余时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 handler.postDelayed 的方法来创建一些动画的东西有一些延迟。
像这样的:

I am using handler.postDelayed method to create some delay for some animation stuff. Like this:

Handler h = new Handler();
h.postDelayed(new Runnable() {
  @Override
  public void run() {
    // Start Animation.
  }
}, 6000);

后来,我怎样才能获得剩余的时间,直到动画开始?

Later, How can I get the remaining time until the animation starts?

推荐答案

您可以简单地节省时间的变种,当你调用后延迟

You can simply save the time in a var when you call post delayed

 long startTime = System.nanoTime();
 h.postDelayed(...

,然后当你需要检查剩余的时间就可以计算出像

and then when you need to check the remaining time you can calculate the elapsed time like

 long elapsedTime = System.nanoTime()-startTime;

所以你的情况

 long remainingTime = 6000 - elapsedTime;

这篇关于使用handler.postDelayed时获取剩余时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆