检测天气用户已在Android上的活动外部向左或向右滑动

时间:2016-08-01 13:59:55

标签: android

我想查看用户在我的应用外部向左或向右滑动的天气。我知道可以onTouchEvent()使用WindowManager进行设置,但我不知道如何这样做我已经通过各种网站和链接,但没有找到适当的解决方案。请帮助我。

我已经使用过此代码,但它只检测了我的活动中的滑动,它没有检测到我应用之外的滑动

 public class Swipe implements View.OnTouchListener {

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



    public Swipe(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 GestureDetector.SimpleOnGestureListener{

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


       @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){
                           Log.i("swipe","right swipe");
                       }
                       else{
                           Log.i("swipe","left swipe");
                       }
                   }
                   result=true;
               }
               result=true;
           }
           catch (Exception e){
               e.printStackTrace();
           }
           return result;
       }
    }


}

0 个答案:

没有答案