问题与屏幕刷卡检测code [英] issue with screen swipe detection code

查看:144
本文介绍了问题与屏幕刷卡检测code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我与下面的code。我用这code检测屏幕上的手指滑过。

Can someone help me with the code below. I am using this code to detect finger swipe on the screen.

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class ActivitySwipeDetector implements View.OnTouchListener {  

 private Activity activity;
 static final int MIN_DISTANCE = 100;
 private float downX, downY, upX, upY;

 public ActivitySwipeDetector(final Activity activity) { 
  this.activity = activity;
 }

 public void onRightToLeftSwipe() {
}

 public void onLeftToRightSwipe(){
}

 public void onTopToBottomSwipe(){
}

 public void onBottomToTopSwipe(){
}

public boolean onTouch(View v, MotionEvent event) {
 switch(event.getAction()){
 case MotionEvent.ACTION_DOWN: {
Log.i("msg", "in MotionEvent.ACTION_DOWN "+event.getX());
downX = event.getX();
downY = event.getY();
 }
 case MotionEvent.ACTION_UP: {
Log.i("msg", "in MotionEvent.ACTION_UP "+event.getX());
upX = event.getX();
upY = event.getY();

 float deltaX = downX - upX;
 float deltaY = downY - upY;

 // swipe horizontal?
   if(Math.abs(deltaX) > MIN_DISTANCE){
  // left or right
  if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
  if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
 } else { Log.i("msg", "X-axis Swipe was only " + downX+"-"+upX + " long, need at least " +     
  MIN_DISTANCE); }

 // swipe vertical?
 if(Math.abs(deltaY) > MIN_DISTANCE){
  // top or down
  if(deltaY < 0) { this.onTopToBottomSwipe(); return true; }
  if(deltaY > 0) { this.onBottomToTopSwipe(); return true; }
 } else { Log.i("msg", "Y-axis Swipe was only " + downY+"-"+upY  + " long, need at least " + 
MIN_DISTANCE); }

 //     return true;
}
}
return false;
 }

 }

这就是我如何在我的code。使用这个类

and this is how I am using this class in my code

  myTextView.setOnTouchListener(new ActivitySwipeDetector(this) {
        public void onRightToLeftSwipe() {
            //do some stuff;
        }
        public void onLeftToRightSwipe() {
            //do some stuff;
        }
        public void onTopToBottomSwipe() {
            //do some stuff;
        }
        public void onBottomToTopSwipe() {
            //do some stuff;
        }
    });

当我测试这个code(直接在手机上,而不是在仿真器)都UPX广告downX变量返回的值相同。同样是upY行柔和值的情况。因此法onLeftToRightSwipe()等不被调用摆在首位。

When I test this code(directly on phone, not on emulator) both the upX ad downX variables are returning same values. Same is the case with upY and downY values. And hence method onLeftToRightSwipe() etc are not being invoked in the first place.

推荐答案

增加一条线返回true;万一MotionEvent.ACTION_DOWN解决了这个问题。

Adding a line "return true;" in case MotionEvent.ACTION_DOWN solved the problem.

这篇关于问题与屏幕刷卡检测code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆