简体   繁体   English

如何在应用程序外部的屏幕上使用检测手指轻扫

[英]how to use detect finger swipes on screen on outside of app

In my app I want to use GestureEvents to detect swipe done by the user like this: 在我的应用中,我想使用GestureEvents来检测用户的滑动,如下所示:

GestureDetector gd=new GestureDetector(this,this); //code is implemented in MainActivity of my app

@Override
public boolean onTouchEvent(MotionEvent event){
    return gd.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

    float sensitivity =50;
    if(e1.getX()-e2.getX()>sensitivity){
        Toast.makeText(MainActivity.this,"left swipe",Toast.LENGTH_SHORT).show();
        return true;
    }
    else if(e2.getX()-e1.getX()>sensitivity){
        Toast.makeText(MainActivity.this,"right swipe",Toast.LENGTH_SHORT).show();
        return true;
    }
   else{
        return true;
    }
}

From the above code I can get to know weather user has swiped left or right on screen.But it detects until my activity is in foreground when my app goes background it doesn't detects any motions done on the screen.I want it to work outside of my app too even if the app is destroyed.Is it possible how can I do that please help me. 从上面的代码中我可以知道天气用户在屏幕上向左或向右滑动,但是当我的应用程序变为后台时它可以检测到我的活动一直处于前台状态,因此它无法检测到屏幕上的任何动作。即使该应用已被破坏,也可能不在我的应用外部。请问该如何帮助我。

Use below code for swipe detection, may be it is helpful to you, in my project working perfectly, so you can try this.. 使用下面的代码进行滑动检测,可能对您有所帮助,因为我的项目运行正常,因此您可以尝试一下。

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;
int SWIPE_THRESHOLD = 200;
int SWIPE_VELOCITY_THRESHOLD = 200;

public OnSwipeTouchListener(Context context) {
    gestureDetector = new GestureDetector(context, new CustomGestureListenerClass());
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class CustomGestureListenerClass extends SimpleOnGestureListener {


    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        singleClicked(e);
        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onScroll(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float distanceX, float distanceY) {
        return super.onScroll(startMotionEvent, endMotionEvent, distanceX, distanceY);
    }


    @Override
    public boolean onFling(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float velocityX, float velocityY) {

        boolean result = false;
        try {
            float diffY = endMotionEvent.getY() - startMotionEvent.getY();
            float diffX = endMotionEvent.getX() - startMotionEvent.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;
            }
            result = true;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
    // if you want done some portion before call this method then write here
}

public void onSwipeLeft() {
    // if you want done some portion before call this method then write here
}

public void singleClicked(MotionEvent e) {
}
}

and configure on activity using below code 并使用以下代码配置活动

getWindow().getDecorView().setOnTouchListener(new OnSwipeTouchListener(this) {
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android - 当用户在屏幕上滑动手指时,如何检测特定视图具有焦点 - android - how to detect a particular view has focus when user swipes finger on screen 触摸如何检测ImageViews如果用户在不同的图像视图上滑动手指 - How to detect ImageViews is Touched If the user swipes his finger on different imageviews 浓咖啡-如何进行两指滑动? - Espresso - How to do 2 finger swipes? android检测用户在图片上滑动手指的方式 - android Detect which way user swipes finger over picture 如何在使用另一个活动滑动屏幕的活动中使用 onBackPressed? - How to use onBackPressed in an activity that swipes the screen with another activity? 如何在自己的应用程序之外检测屏幕手势? - How can one detect screen gestures outside ones own app? 如何在Android touchlistener中同时检测到触摸屏幕上的次要手指? - how to detect the secondary finger on touching the screen not at same time in android touchlistener? ItemTouchHelper:如何在手指超过滑动阈值之前防止“快速”滑动? - ItemTouchHelper: How to prevent "fling" swipes until finger passes swipe threshold? 如何在Flash中同时检测两次滑动? - How to detect two simultaneous swipes in flash? 检测手指何时离开屏幕并更新指针 - detect when a finger leaves the screen and update pointers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM