gpt4 book ai didi

android - 移动几个单位后的触摸事件

转载 作者:行者123 更新时间:2023-11-29 18:04:56 25 4
gpt4 key购买 nike

我正在尝试制作一个触摸事件,直到手指从初始位置移动几个单位后才会被激活。

到目前为止,我已经像这样设置了我的 onTouch 方法:

private XYEvents xyEvent =  new XYEvents();

public boolean motionTracker(MotionEvent event, int n)
{
int note = n;

switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
xyEvent.setInitial(event);
playNote(note);

break;


case MotionEvent.ACTION_MOVE:
byte data1;
byte data2;

//I figured I should input a condition to check if the finger has moved a few units before it should start doing stuff like so:

if (xyEvent.getXThreshold(event))
{
int xMod = xyEvent.eventActions(event)[0];

data1 = (byte) (xMod & 0x7f);
data2 = (byte) ((xMod & 0x7f00) >> 8);
xModulation((int)data1, (int)data2);
}


break;
}

这个方法是我遇到的问题:

private float initialX, initialY;
private int xValue;

boolean getXThreshold(MotionEvent event)
{
float deltaX = event.getX();
float threshold = 10;

float condition = (deltaX - initialX);

if(condition <= threshold || condition >= -threshold )
return false;
else
return true;
}

getXThreshold 方法似乎在另一个看起来像这样的方法中完成了它应该做的事情:

public int[] eventActions(MotionEvent event)
{
int value = xValue;

int xNull = 8192;


if(!getXThreshold(event))
xValue = xNull;


if(getXThreshold(event))
xValue = xHandleMove(event, true);


return value;


}

有什么建议吗?

/M

最佳答案

这个说法好像是:

if(condition <= threshold || condition >= -threshold )
return false;
else
return true;

需要翻转,否则总是因为某些原因返回false。

现在它看起来像这样并且效果很好。

boolean getXThreshold(MotionEvent event)
{
float deltaX = event.getX();
float threshold = 10;

float condition = (deltaX - initialX);

return condition >= threshold || condition <= -threshold;
}

祝你度过愉快的一周!/M

关于android - 移动几个单位后的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13880417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com