在 webview 上检测单击
Detect Single Tap on webview
我在 webview 中使用运动事件,所以我不能直接在 touchlistener 上使用
我正在使用以下方式
package com.example.webviewdemo;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
private long touchDownMs;
private Handler handler;
private int numberOfTaps;
private long lastTapTimeMs;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public void newTouch(){
}
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
handler= new Handler();
if(event.getAction()== MotionEvent.ACTION_MOVE)
{
return true;
}
if(event.getAction()== MotionEvent.ACTION_DOWN){
touchDownMs = System.currentTimeMillis();
return v.onTouchEvent(event);
}
else if(event.getAction()== MotionEvent.ACTION_UP){
handler.removeCallbacksAndMessages(null);
if ((System.currentTimeMillis() - touchDownMs) > ViewConfiguration.getTapTimeout()) {
//it was not a tap
numberOfTaps = 0;
lastTapTimeMs = 0;
}
if (numberOfTaps > 0
&& (System.currentTimeMillis() - lastTapTimeMs) < ViewConfiguration.getDoubleTapTimeout()) {
numberOfTaps += 1;
} else {
numberOfTaps = 1;
}
lastTapTimeMs = System.currentTimeMillis();
if (numberOfTaps == 3) {
// Toast.makeText(getApplicationContext(), "triple", Toast.LENGTH_SHORT).show();
//handle triple tap
} else if (numberOfTaps == 2) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
//handle double tap
newTouch();
// Toast.makeText(getApplicationContext(), "double", Toast.LENGTH_SHORT).show();
}
}, ViewConfiguration.getDoubleTapTimeout());
}
return v.onTouchEvent(event);
}
else{
return v.onTouchEvent(event);
}
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
在我的 webview 上实现
webView.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeLeft() {
// Whatever
}
@Override
public void onSwipeRight() {
// TODO Auto-generated method stub
}
@Override
public void newTouch() {
// TODO Auto-generated method stub
}
});
我可以从中获得双击和三次点击,但我只需要一次完整的点击,而不仅仅是 ACTION_UP 或 ACTION_DOWN,但两者都在毫秒间隔内。
如果在我的代码中我写了 numberOfTaps ==1 那么它检测到 Action_Down 没有完成点击。我需要完成 1 次点击检测。
提前致谢
我已经看过 This and This 但没用
package com.example.webviewdemo;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
private static final int MAX_CLICK_DURATION = 1000;
private static final int MAX_CLICK_DISTANCE = 15;
static Context mcontext;
private long pressStartTime;
private float pressedX;
private float pressedY;
private boolean stayedWithinClickDistance;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
this.mcontext=context;
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public void newTouch(){
}
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
if(event.getAction()== MotionEvent.ACTION_MOVE)
{
if (stayedWithinClickDistance && distance(pressedX, pressedY, event.getX(), event.getY()) > MAX_CLICK_DISTANCE) {
stayedWithinClickDistance = false;
}
return true;
}
else if (event.getAction()== MotionEvent.ACTION_DOWN) {
pressStartTime = System.currentTimeMillis();
pressedX = event.getX();
pressedY = event.getY();
stayedWithinClickDistance = true;
return v.onTouchEvent(event);
}
else if(event.getAction()== MotionEvent.ACTION_UP) {
long pressDuration = System.currentTimeMillis() - pressStartTime;
if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
newTouch();
}
return v.onTouchEvent(event);
}
else{
return v.onTouchEvent(event);
}
}
private static float distance(float x1, float y1, float x2, float y2) {
float dx = x1 - x2;
float dy = y1 - y2;
float distanceInPx = (float) Math.sqrt(dx * dx + dy * dy);
return pxToDp(distanceInPx);
}
private static float pxToDp(float px) {
return px / mcontext.getResources().getDisplayMetrics().density;
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
使用这个 How to distinguish between move and click in onTouchEvent()?
解决了它
我在 webview 中使用运动事件,所以我不能直接在 touchlistener 上使用
我正在使用以下方式
package com.example.webviewdemo;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
private long touchDownMs;
private Handler handler;
private int numberOfTaps;
private long lastTapTimeMs;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public void newTouch(){
}
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
handler= new Handler();
if(event.getAction()== MotionEvent.ACTION_MOVE)
{
return true;
}
if(event.getAction()== MotionEvent.ACTION_DOWN){
touchDownMs = System.currentTimeMillis();
return v.onTouchEvent(event);
}
else if(event.getAction()== MotionEvent.ACTION_UP){
handler.removeCallbacksAndMessages(null);
if ((System.currentTimeMillis() - touchDownMs) > ViewConfiguration.getTapTimeout()) {
//it was not a tap
numberOfTaps = 0;
lastTapTimeMs = 0;
}
if (numberOfTaps > 0
&& (System.currentTimeMillis() - lastTapTimeMs) < ViewConfiguration.getDoubleTapTimeout()) {
numberOfTaps += 1;
} else {
numberOfTaps = 1;
}
lastTapTimeMs = System.currentTimeMillis();
if (numberOfTaps == 3) {
// Toast.makeText(getApplicationContext(), "triple", Toast.LENGTH_SHORT).show();
//handle triple tap
} else if (numberOfTaps == 2) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
//handle double tap
newTouch();
// Toast.makeText(getApplicationContext(), "double", Toast.LENGTH_SHORT).show();
}
}, ViewConfiguration.getDoubleTapTimeout());
}
return v.onTouchEvent(event);
}
else{
return v.onTouchEvent(event);
}
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
在我的 webview 上实现
webView.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeLeft() {
// Whatever
}
@Override
public void onSwipeRight() {
// TODO Auto-generated method stub
}
@Override
public void newTouch() {
// TODO Auto-generated method stub
}
});
我可以从中获得双击和三次点击,但我只需要一次完整的点击,而不仅仅是 ACTION_UP 或 ACTION_DOWN,但两者都在毫秒间隔内。 如果在我的代码中我写了 numberOfTaps ==1 那么它检测到 Action_Down 没有完成点击。我需要完成 1 次点击检测。 提前致谢
我已经看过 This and This 但没用
package com.example.webviewdemo;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
private static final int MAX_CLICK_DURATION = 1000;
private static final int MAX_CLICK_DISTANCE = 15;
static Context mcontext;
private long pressStartTime;
private float pressedX;
private float pressedY;
private boolean stayedWithinClickDistance;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
this.mcontext=context;
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public void newTouch(){
}
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
if(event.getAction()== MotionEvent.ACTION_MOVE)
{
if (stayedWithinClickDistance && distance(pressedX, pressedY, event.getX(), event.getY()) > MAX_CLICK_DISTANCE) {
stayedWithinClickDistance = false;
}
return true;
}
else if (event.getAction()== MotionEvent.ACTION_DOWN) {
pressStartTime = System.currentTimeMillis();
pressedX = event.getX();
pressedY = event.getY();
stayedWithinClickDistance = true;
return v.onTouchEvent(event);
}
else if(event.getAction()== MotionEvent.ACTION_UP) {
long pressDuration = System.currentTimeMillis() - pressStartTime;
if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
newTouch();
}
return v.onTouchEvent(event);
}
else{
return v.onTouchEvent(event);
}
}
private static float distance(float x1, float y1, float x2, float y2) {
float dx = x1 - x2;
float dy = y1 - y2;
float distanceInPx = (float) Math.sqrt(dx * dx + dy * dy);
return pxToDp(distanceInPx);
}
private static float pxToDp(float px) {
return px / mcontext.getResources().getDisplayMetrics().density;
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
使用这个 How to distinguish between move and click in onTouchEvent()?
解决了它