Posted on 2010-12-03 10:34
啥都寫點 閱讀(549)
評論(0) 編輯 收藏 所屬分類:
Android
對于游戲開發中我們可能經常要用到雙按屏幕,在Android 1.6以前并沒有提供完善的手勢識別類,在Android 1.5 SDK中我們可以找到android.view.GestureDetector.OnDoubleTapListener,但是經過測試仍然無法正常工作,不知道什么原因,如果您知道可以聯系android123@163.com 共享下。最終我們使用的解決方法如下
最終我們測試的如下:
view plaincopy to clipboardprint?
public class TouchLayout extends RelativeLayout {
public Handler doubleTapHandler = null;
protected long lastDown = -1;
public final static long DOUBLE_TIME = 500;
public TouchLayout(Context context) {
super(context);
}
public TouchLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean onTouchEvent(MotionEvent event) {
this.handleEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
long nowDown = System.currentTimeMillis();
if (nowDown - lastDown < DOUBLE_TIME)
{
if (doubleTapHandler != null)
doubleTapHandler.sendEmptyMessage(-1);
} else {
lastDown = nowDown;
}
}
return true;
}
protected void handleEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Do sth 這里處理即可
break;
case MotionEvent.ACTION_UP:
//Do sth
break;
}
}
}
public class TouchLayout extends RelativeLayout {
public Handler doubleTapHandler = null;
protected long lastDown = -1;
public final static long DOUBLE_TIME = 500;
public TouchLayout(Context context) {
super(context);
}
public TouchLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean onTouchEvent(MotionEvent event) {
this.handleEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
long nowDown = System.currentTimeMillis();
if (nowDown - lastDown < DOUBLE_TIME)
{
if (doubleTapHandler != null)
doubleTapHandler.sendEmptyMessage(-1);
} else {
lastDown = nowDown;
}
}
return true;
}
protected void handleEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Do sth 這里處理即可
break;
case MotionEvent.ACTION_UP:
//Do sth
break;
}
}
}
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/JavaTiger427/archive/2010/11/25/6034698.aspx
--
學海無涯