0
我想要一个整个屏幕的OnTouchListener。我已经尝试将所有视图附加到onTouchListener,但生成不好的touchEvents。我知道这可以通过重写方法来实现,我正在寻找的是一个监听器解决方案。谢谢!整个屏幕的OnTouchListener
这可以使用手势监听器来完成吗?
我想要一个整个屏幕的OnTouchListener。我已经尝试将所有视图附加到onTouchListener,但生成不好的touchEvents。我知道这可以通过重写方法来实现,我正在寻找的是一个监听器解决方案。谢谢!整个屏幕的OnTouchListener
这可以使用手势监听器来完成吗?
您可以插入一个onSwipeListener类,其中包含侦听滑动操作的方法。然后,您可以为activity的布局(LinearLayout/RelativeLayout)设置view.OnTouchListener,然后覆盖onSwipeListener的各种方法并插入各种任务。
下面是您可以创建的onSwipeListener类。
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_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) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
result = true;
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
result = true;
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
创建这个类之后,您可以按以下称之为:
relativeLayout.setOnTouchListener(new OnSwipeTouchListener(context) {
public void onSwipeRight() {
//do something
}
public void onSwipeLeft() {
//do something
}
}
希望这有助于!
下面的链接解释了如何使用图像进行操作:http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-2-building -the-touch-example/1763有人在SO有类似的问题:http://stackoverflow.com/questions/5648985/ontouchlistener-for-entire-screen至于手势监听器的实现,我不知道。 – ihtkwot
是否可以在所有视图上制作透明叠加视图,然后只听叠置视图?如果我点击这个叠加的视图,点击会通过像按钮一样的下面的视图? –
我不确定迈克。我没有尝试过自己。我只是试图捡拾一些我在找到问题答案时找到的方法。对不起,我不能更有帮助。 – ihtkwot