如何找到android中的action up和action down事件之间的时间间隔

发布于 2024-11-29 23:11:34 字数 305 浏览 0 评论 0原文

我有以下代码...

public boolean onTouch(View view, MotionEvent me) {

    //here i want to know the  time of touch

    switch (me.getAction()) {
       case MotionEvent.ACTION_DOWN: { }  
       case MotionEvent.ACTION_UP: { }
       -
       -
       -
   }
  }

有什么想法吗?

i have following code...

public boolean onTouch(View view, MotionEvent me) {

    //here i want to know the  time of touch

    switch (me.getAction()) {
       case MotionEvent.ACTION_DOWN: { }  
       case MotionEvent.ACTION_UP: { }
       -
       -
       -
   }
  }

Any Idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北风几吹夏 2024-12-06 23:11:35

这很简单

public yourClass{
    long down;    
    public boolean onTouch(View view, MotionEvent me) {
        //you don't need here the difference because it might be a down action!
        //you need the difference when the up action occurs
        switch (me.getAction()) {
            case MotionEvent.ACTION_DOWN:
                down = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_UP:
                //this is the time in milliseconds
                long diff = System.currentTimeMillis() - down; 
                break;
        }
    }
}     

It is pretty simple

public yourClass{
    long down;    
    public boolean onTouch(View view, MotionEvent me) {
        //you don't need here the difference because it might be a down action!
        //you need the difference when the up action occurs
        switch (me.getAction()) {
            case MotionEvent.ACTION_DOWN:
                down = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_UP:
                //this is the time in milliseconds
                long diff = System.currentTimeMillis() - down; 
                break;
        }
    }
}     
凉风有信 2024-12-06 23:11:35

我想您想测量两个事件之间的时间。这很简单。您所需要的只是找到一个返回当前时间的 API 方法。在这种情况下 android.os.SystemClock.elapsedRealtime() 或 .uptimeMillis() 应该为您服务(Android 文档)。
当第一个事件发生时,您将当前时间保存在变量中。在第二个事件中,您只需计算当前时间与之前存储的值之间的差异。

I suppose you want to measure the time between the two events. This is very simple. All you need is to find an API method which returns the current time. In this case android.os.SystemClock.elapsedRealtime() or .uptimeMillis() should serve you (android documentation).
When the first event occurs, you save the current time in a variable. On the second event you just calculate the difference between the current time and the value you stored before.

演出会有结束 2024-12-06 23:11:35

您可以用来

System.currentTimeMillis();

捕获每个事件的时间

You can use

System.currentTimeMillis();

to capture the times on each event

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文