查找Android的触摸事件之间的时间间隔 [英] Find duration between touch events in android

查看:403
本文介绍了查找Android的触摸事件之间的时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个 onTouchListener ,允许用户点击 textView2 整整10倍,如下图所示。我的目标是测量触摸1和2的触摸之间的时间,并将其存储为一个变量,比如时间1 。不过,我不太清楚如何做到这一点。我有一个想法是建立一个变量, I ,即测量被点击 TouchListener 的次数。我在想可能测量的的时候,我包含一个特定的值(例如,如果 I 等于1为1秒,这意味着触摸1和触摸2之间的时间为1秒)。但是我不知道如何实现这一点,我甚至不知道这是否是正确的方法。有谁知道如何解决这个问题?

的.java 文件

 公共类MainActivity延伸活动{INT I;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    最终的TextView textView2 =(的TextView)findViewById(R.id.textView2);    I = 0;    textView2.setOnTouchListener(新View.OnTouchListener(){
        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            如果(event.getAction()== MotionEvent.ACTION_DOWN){
                我++;
                如果(我== 10)textView2.setOnTouchListener(NULL);            }
            返回false;
        }
    });}


解决方案

在你的类

 私人长期pressTime = -1L;
私人长期releaseTime = 1 L;
私人持续时间长= -1L;

然后在你的onTouch方法

 如果(event.getAction()== MotionEvent.ACTION_DOWN){
     pressTime = System.currentTimeMillis的();
     如果(!releaseTime = -1L)持续时间= pressTime - releaseTime;
}
否则如果(event.getAction()== MotionEvent.ACTION_UP){
     releaseTime = System.currentTimeMillis的();
     持续时间= System.currentTimeMillis的() - pressTime;
}

现在你有触摸事件之间的时间:


  • 持续时间,当你preSS下来就是你释放的最后时间和当前preSS之间的时间(如果previously pressed下来并释放按钮)。

  • 持续时间,当你释放你是pressed下来的最后时间和当前释放时间之间的时间。

- 编辑 -

如果您需要知道所有事件的时间差,你可以做类似

 私人长期lastEvent = -1L;
私人持续时间长= -1L;

然后在onTouch事件

 如果(!lastEvent = -1L)持续时间= System.currentTimeMillis的() -  lastEvent;
lastEvent = System.currentTimeMillis的();

您也可以创建持续时间的列表

 私人列表<龙>持续时间=新的ArrayList<龙>();

和在onTouch,而不是持续时间= ... DO

  durations.add(System.currentTimeMillis的() -  lastEvent);

这可能是检查所有序列事件之间的所有持续时间非常有用。例如,如果你想知道$ P $之间的时间pssing下来,拖,停止拖动,开始拖动,然后抬起你抬起每一个时间后有问题,而不必不断地检查你可以检查你的清单单时间。

I have set up an onTouchListener which allows the user to click textView2 exactly 10 times, as shown below. My goal is to measure the time between touch 1 and touch 2, and store it as a variable, say time1. However, I'm not quite sure how to do this. One idea I had was setting up a variable, i, that measures the number of times the TouchListener was clicked. I was thinking of potentially measuring the time that i contained a particular value (for example, if i was equal to 1 for 1 second, this means the time between touch 1 and touch 2 was 1 second). However I'm not sure how to implement this, and I'm not even sure if this is the correct method. Does anyone know how to solve this problem?

.java file

public class MainActivity extends Activity {

int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView textView2 = (TextView)findViewById(R.id.textView2);

    i=0;

    textView2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                i++;
                if (i==10) textView2.setOnTouchListener(null);

            }
            return false;
        }
    });         

}

解决方案

In your class

private long pressTime = -1l;
private long releaseTime = 1l;
private long duration = -1l;

Then in your onTouch method

if(event.getAction() == MotionEvent.ACTION_DOWN){
     pressTime = System.currentTimeMillis();
     if(releaseTime != -1l) duration = pressTime - releaseTime;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
     releaseTime = System.currentTimeMillis();
     duration = System.currentTimeMillis() - pressTime;
}

Now you have your duration between touch events:

  • Duration when you press down is the time between the last time you released and the current press (if you have previously pressed down and released the button).
  • Duration when you release is the time between the last time you pressed down and the current release time.

-Edit-

If you need to know the difference in time of all events you can just do something like

private long lastEvent = -1l;
private long duration = -1l;

Then in onTouch event

if(lastEvent != -1l) duration = System.currentTimeMillis() - lastEvent;
lastEvent = System.currentTimeMillis();

You can also create a list of durations

private List<Long> durations = new ArrayList<Long>();

and in onTouch instead of duration = ... do

durations.add(System.currentTimeMillis() - lastEvent); 

This could be useful for checking all durations between all sequential events. For example, if you want to know the time between pressing down, dragging, stopping dragging, starting dragging, and then lifting up you could check your list after you lift up for every time in question instead of having to constantly check a single duration.

这篇关于查找Android的触摸事件之间的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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