Android 如何在不抬起手指的情况下检测刷卡事件
我想在连续向上/向下滑动事件中增加/减少一个数字。 我可以使用GestureDetector和OnSwipeTouchListener实现简单的滑动事件 但是,只有在刷卡后抬起手指时,才会感测到此刷卡事件。 因此,为了将数字增加到+5,我必须在屏幕上进行5次单独的向上滑动 我想应用类似“滑块”的动作,这样当用户在屏幕上任意位置上下滑动时,数字就会改变。(我不想使用“滑块”小部件) 这可能吗 任何帮助都会非常感激。Android 如何在不抬起手指的情况下检测刷卡事件,android,Android,我想在连续向上/向下滑动事件中增加/减少一个数字。 我可以使用GestureDetector和OnSwipeTouchListener实现简单的滑动事件 但是,只有在刷卡后抬起手指时,才会感测到此刷卡事件。 因此,为了将数字增加到+5,我必须在屏幕上进行5次单独的向上滑动 我想应用类似“滑块”的动作,这样当用户在屏幕上任意位置上下滑动时,数字就会改变。(我不想使用“滑块”小部件) 这可能吗 任何帮助都会非常感激。 谢谢 使用OnTouchListener: private float baseX
谢谢 使用OnTouchListener:
private float baseX, baseY;
OnTouchListener listener = new OnTouchListener(){
public boolean onTouch (View v, MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
//TOUCH STARTED
baseX = event.getX();
baseY = event.getY();
return true;
case MotionEvent.ACTION_MOVE:
//FINGER IS MOVING
//Do your calculations here, using the x and y positions relative to the starting values you get in ACTION_DOWN
return true;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
//TOUCH COMPLETED
return true;
}
}
}
@1up,非常感谢您提供的解决方案 我可以添加的一个增强功能是,即使在不抬起手指的情况下反转滑动方向,也会很好地工作 使用您提供的框架,发布我的代码,希望它能在将来帮助某人
public class OnSwipeListener implements OnTouchListener
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
boolean result = false;
float currentX = 0.0f;
float currentY = 0.0f;
float current_diffX = 0.0f;
float current_diffY = 0.0f;
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
//Touch detected, record base X-Y coordinates
baseX = event.getX();
baseY = event.getY();
//As the event is consumed, return true
result = true;
break;
case MotionEvent.ACTION_MOVE:
//Swipe started, get current X-Y coordinates and compute those with the base ones
currentX = event.getX();
currentY = event.getY();
current_diffX = currentX - baseX;
current_diffY = currentY - baseY;
//...................................................Determine horizontal swipe direction
if(h_swipe == LEFT_SWIPE)
{
if( currentX > previousX )
{
//If here, then horizontal swipe has been reversed
h_swipe = RIGHT_SWIPE;
//Overwrite base coordinate
baseX = previousX;
//Recalculate Difference
current_diffX = currentX - baseX;
}
else
{
//NOP - Intentionally kept empty
}
}
else if(h_swipe == RIGHT_SWIPE)
{
if( currentX < previousX )
{
//If here, then horizontal swipe has been reversed
h_swipe = LEFT_SWIPE;
//Overwrite base coordinate
baseX = previousX;
//Recalculate Difference
current_diffX = currentX - baseX;
}
else
{
//NOP - Intentionally kept empty
}
}
else
{
//If here, then it's a fresh swipe event, so compare with base coordinates
if( currentX < baseX )
{
h_swipe = LEFT_SWIPE;
}
else if( currentX > baseX )
{
h_swipe = RIGHT_SWIPE;
}
else
{
//NOP - Intentionally kept empty
}
}
//...................................................Determine vertical swipe direction
if(v_swipe == UP_SWIPE)
{
if(currentY > previousY)
{
//If here, then vertical swipe has been reversed
v_swipe = DOWN_SWIPE;
//Overwrite base coordinate
baseY = previousY;
//Recalculate coordinate difference
current_diffY = currentY - baseY;
}
else
{
//NOP - Intentionally kept empty
}
}
else if(v_swipe == DOWN_SWIPE)
{
if(currentY < previousY)
{
//If here, then vertical swipe has been reversed
v_swipe = UP_SWIPE;
//Overwrite base coordinate
baseY = previousY;
//Recalculate coordinate difference
current_diffY = currentY - baseY;
}
else
{
//NOP - Intentionally kept empty
}
}
else
{
//If here, then it's a fresh swipe event, so compare with base coordinates
if( currentY < baseY )
{
v_swipe = UP_SWIPE;
}
else if( currentY > baseY )
{
v_swipe = DOWN_SWIPE;
}
else
{
//NOP - Intentionally kept empty
}
}
//Record current coordinates for future comparisons
previousX = currentX;
previousY = currentY;
//................................Determine the prominent swipe (horizontal/vertical)
if(Math.abs(current_diffX) > Math.abs(current_diffY))
{
//It's a horizontal swipe
if (Math.abs(current_diffX) > SWIPE_THRESHOLD)
{
if (current_diffX > 0)
{
onRightSwipe();
}
else
{
onLeftSwipe();
}
}
else
{
//Not enough swipe movement, ignore.
//NOP - Intentionally kept empty
}
}
else
{
//It's a vertical swipe
if (Math.abs(current_diffY) > SWIPE_THRESHOLD)
{
if (current_diffY > 0)
{
onDownSwipe();
}
else
{
onUpSwipe();
}
}
else
{
//Not enough swipe movement, ignore.
//NOP - Intentionally kept empty
}
}
//As the event is consumed, return true
result = true;
break;
case MotionEvent.ACTION_UP:
//Swipe ended, clear variables, if necessary
h_swipe = NO_H_SWIPE;
v_swipe = NO_V_SWIPE;
baseX = 0.0f;
baseY = 0.0f;
previousX = 0.0f;
previousY = 0.0f;
//As the event is consumed, return true
result = true;
break;
default:
//Do not consume event
result = false;
break;
}
return result;
}
public void onUpSwipe()
{
}
public void onDownSwipe()
{
}
public void onRightSwipe()
{
}
public void onLeftSwipe()
{
}
}
公共类OnSwipeListener实现OnTouchListener
{
@凌驾
公共布尔onTouch(视图v,运动事件)
{
布尔结果=假;
浮动电流X=0.0f;
浮动电流Y=0.0f;
浮动电流_diffX=0.0f;
浮动电流_diffY=0.0f;
开关(event.getAction())
{
case MotionEvent.ACTION\u DOWN:
//检测到触摸,记录基本X-Y坐标
baseX=event.getX();
baseY=event.getY();
//当事件被消耗时,返回true
结果=真;
打破
case MotionEvent.ACTION\u移动:
//开始滑动,获取当前的X-Y坐标,并使用基准坐标计算这些坐标
currentX=event.getX();
currentY=event.getY();
电流_diffX=电流x-基准x;
current_diffY=currentY-baseY;
//确定水平滑动方向
如果(h_滑动==左滑动)
{
如果(当前X>上一个X)
{
//如果在此处,则水平滑动已反转
h_滑动=右滑动;
//覆盖基准坐标
baseX=上一个x;
//重新计算差额
电流_diffX=电流x-基准x;
}
其他的
{
//否-故意保持为空
}
}
else if(h_滑动==右滑动)
{
如果(当前x<先前x)
{
//如果在此处,则水平滑动已反转
h_滑动=左滑动;
//覆盖基准坐标
baseX=上一个x;
//重新计算差额
电流_diffX=电流x-基准x;
}
其他的
{
//否-故意保持为空
}
}
其他的
{
//如果在这里,那么它是一个新的刷卡事件,所以与基本坐标比较
如果(电流xbaseX)
{
h_滑动=右滑动;
}
其他的
{
//否-故意保持为空
}
}
//确定垂直滑动方向
如果(v_滑动==向上滑动)
{
如果(当前>以前)
{
//如果在此处,则垂直滑动已反转
v_滑动=向下滑动;
//覆盖基准坐标
baseY=先前的;
//重新计算坐标差
current_diffY=currentY-baseY;
}
其他的
{
//否-故意保持为空
}
}
否则如果(v_滑动==向下滑动)
{
如果(当前<先前)
{
//如果在此处,则垂直滑动已反转
v_滑动=向上滑动;
//覆盖基准坐标
baseY=先前的;
//重新计算坐标差
current_diffY=currentY-baseY;
}
其他的
{
//否-故意保持为空
}
}
其他的
{
//如果在这里,那么它是一个新的刷卡事件,所以与基本坐标比较
如果(当前Y<基本Y)
{
v_滑动=向上滑动;
}
否则如果(当前Y>基本Y)
{
v_滑动=向下滑动;
}
其他的
{
//否-故意保持为空
}
}
//记录当前坐标以备将来比较
previousX=当前X;
先前的y=当前的y;
//确定突出的侧击(水平/垂直)
if(Math.abs(current_diffX)>Math.abs(current_diffY))
{
//这是横扫
if(Math.abs(current\u diffX)>滑动阈值)
{
如果(当前_diffX>0)
{
onRightSwipe();
}
其他的
{
onleftsweep();
}
}
其他的
import android.view.MotionEvent;
import android.view.View;
public class SwipeDirectionListener implements View.OnTouchListener {
float deltaX ;
float deltaY ;
float maxValX;
float maxValY;
float firstTouchX;
float firstTouchY;
float currentX ;
float currentY ;
float SWIPE_THRESHOLD = 10.0f;
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean result ;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Register the first touch on TouchDown and this should not change unless finger goes up.
firstTouchX = event.getX();
firstTouchY = event.getY();
maxValX = 0.0f;
maxValY = 0.0f;
//As the event is consumed, return true
result = true;
break;
case MotionEvent.ACTION_MOVE:
//CurrentX/Y are the continues changing values of one single touch session. Change
//when finger slides on view
currentX = event.getX();
currentY = event.getY();
//setting the maximum value of X or Y so far. Any deviation in this means a change of direction so reset the firstTouch value to last known max value i.e MaxVal X/Y.
if(maxValX <currentX){
maxValX = currentX;
}else{
firstTouchX= maxValX;
maxValX =0.0f;
}
if(maxValY <currentY){
maxValY = currentY;
}else{
firstTouchY= maxValY;
maxValY =0.0f;
}
//DeltaX/Y are the difference between current touch and the value when finger first touched screen.
//If its negative that means current value is on left side of first touchdown value i.e Going left and
//vice versa.
deltaX = currentX - firstTouchX;
deltaY = currentY - firstTouchY;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
//Horizontal swipe
if (Math.abs(deltaX) > SWIPE_THRESHOLD) {
if (deltaX > 0) {
//means we are going right
onRightSwipe(currentX);
} else {
//means we are going left
onLeftSwipe(currentX);
}
}
} else {
//It's a vertical swipe
if (Math.abs(deltaY) > SWIPE_THRESHOLD) {
if (deltaY > 0) {
//means we are going down
onDownSwipe(currentY);
} else {
//means we are going up
onUpSwipe(currentY);
}
}
}
result = true;
break;
case MotionEvent.ACTION_UP:
//Clean UP
firstTouchX = 0.0f;
firstTouchY = 0.0f;
result = true;
break;
default:
result = false;
break;
}
return result;
}
public void onUpSwipe(float value) {
}
public void onDownSwipe(float value) {
}
public void onRightSwipe(float value) {
}
public void onLeftSwipe(float value) {
}
}
yourView.setOnTouchListener(new SwipeDirectionListener() {
public void onUpSwipe(float value) {
mTextView.setText("onUpSwipe :"+value);
}
public void onDownSwipe(float value) {
mTextView.setText("onDownSwipe :"+value);
}
public void onRightSwipe(float value) {
mTextView.setText("onRightSwipe :"+value);
}
public void onLeftSwipe(float value) {
mTextView.setText("onLeftSwipe :"+value);
}
});