长触摸在surfaceView(机器人) [英] Long Touch on a surfaceView ( android )
问题描述
我正在做一个游戏在Android上,我需要做某些动作时用户试图在屏幕上长preSS。不幸的是我还没有发现直接与自定义的SurfaceView的任何方法,觉得免费的,如果这样的方法存在着告诉我:)
I'm making a game on Android and I need to do certain actions when the users attempts a long press on the screen. Unfortunately I haven't found any methods that works directly with a custom SurfaceView, feel free to tell me if such a method exists :)
所以,我决定尝试并实现长触摸检测从onTouch事件侦听器。
So I decided to try and implement a long touch detection from the onTouch event listener.
这是我的code:
@Override
public boolean onTouch(View v, MotionEvent event)
{
long touchDuration = 0;
if ( event.getAction() == MotionEvent.ACTION_DOWN )
{
//Start timer
touchTime = System.currentTimeMillis();
}else if ( event.getAction() == MotionEvent.ACTION_UP )
{
//stop timer
touchDuration = System.currentTimeMillis() - touchTime;
if ( touchDuration < 800 )
{
onShortTouch(event,touchDuration);
}else
{
onLongTouch(event,touchDuration);
}
}
}
return true;
这工作,但只有当用户停止触摸手机,我可以检测,如果preSS是一个漫长的preSS与否。所以,这不正是我想要的。我想preFER如果当用户第一次触摸屏幕那么一旦800毫秒经过的LongTouch()方法被调用一个计时器就会启动。换句话说,我不希望有检查,因为ACTION_DOWN已经多久经过时ACTION_UP。我相信我应该使用一个线程,该计时器,但我不能使它发挥作用。当采用下面code,是当我触摸屏幕上显示的调试消息:
This works, but I can detect if the press was a long press or not only when the user stops touching the phone. So it's not exactly what I want. I would prefer if a timer would start when the user first touches the screen then once 800 ms is elapsed the LongTouch() method is called. In other words, I don't want to have to check how long has elapsed when ACTION_UP since ACTION_DOWN. I believe I should use a thread for the said timer but I can't make it work. When using the following code, the debug message is displayed as soon as I touch the screen:
@Override
public boolean onTouch(View v, MotionEvent event)
{
long touchDuration = 0;
TouchThread touchThread = new TouchThread();
if ( event.getAction() == MotionEvent.ACTION_DOWN )
{
//Start timer
touchTime = System.currentTimeMillis();
touchThread.setEvent(event);
touchThread.run();
}
return true;
}
private class TouchThread extends Thread
{
public MotionEvent event = null;
public void setEvent(MotionEvent e)
{
event = e;
}
@Override
public void run()
{
long startTime = System.currentTimeMillis();
long time = 0;
while(event.getAction() == MotionEvent.ACTION_DOWN)
{
time = System.currentTimeMillis() - startTime;
if( time > 800 )
{
System.out.println("LOOONG CLICK!!");
return;
}
}
}
}
有没有人有任何想法?另一个解决方法也将受到欢迎。
Has anyone got any idea ? An other solution would also be welcome.
感谢。
推荐答案
当你也希望做一些手工的事件处理一个很好的选择是GestureDetector类:的 http://developer.android.com/reference/android/view/GestureDetector.html
A nice option when you also want to do some manual event processing is the GestureDetector class: http://developer.android.com/reference/android/view/GestureDetector.html
我如何使用它是让GestureDetector实例,在构造函数中,实现我感兴趣的方法,然后在的onTouchEvent()方法的开始做这样的事情:
How I use it is to make a GestureDetector instance in the constructor, implement the methods I'm interested in, and then at the start of the onTouchEvent() method do something like:
if (gestureDetector.onTouchEvent(event)) {
return true;
}
哪个然后在大多数情况下是指当检测到的姿态是中止的你的触摸操作的其余部分。然而,长preSS是有点不同,因为从长远preSS方法不返回一个值。在这种情况下,我只是说我设置的时候长preSS被触发,我在触摸事件处理方法,如果以后需要检查一个布尔值的地方。
Which then means in most cases when a gesture is detected that aborts the rest of your touch handling. However, long press is a little different because the long press method doesn't return a value. In this case I just have a boolean somewhere that I set when long press is triggered that I check later in the touch event handling method if necessary.
这篇关于长触摸在surfaceView(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!