我正在尝试简单的程序,如果移动有声音。所以在开始的时候我已经失声了 - 播放声音1然后每次移动它都会播放声音。在第4阶段,我从头开始玩。
问题在于:当我不移动手指并将其保持在同一位置时,声音仍然保持1比1 - 计算出x和y值的触发。我怎么阻止这个?
OnTouchListener MyOnTouchListener= new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
switch(event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
x = (int) event.getX();
y = (int) event.getY();
oldval = x+y;
break;
case MotionEvent.ACTION_MOVE:
{
Log.e("X value", "X is "+x);
Log.e("Y value", "Y is "+y);
try
{
Thread.sleep(500);
} catch (InterruptedException e) {
}
int newval= (int) (event.getX() + event.getY());
if(Math.abs(oldval-newval)>50)
{
Log.e("First", "next button");
longpressCount++;
if(longpressCount==1)
{
Log.e("1", "BUTTON PRESSED");
}
else if(longpressCount==2)
{
Log.e("2", "BUTTON PRESSED");
}
else if(longpressCount==3)
{
Log.e("3", "BUTTON PRESSED");
}
else if(longpressCount==4)
{
Log.e("4", "BUTTON PRESSED");
longpressCount = 0;
}
}
break;
}
}
return true;
}
答案 0 :(得分:11)
MOVE是非常敏感的,只要你的手指向下,它就会继续被调用。在声音播放代码的末尾设置旧值,这样只有在距离该位置再移动50个距离时才会播放。
像这样。
OnTouchListener MyOnTouchListener= new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
switch(event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
x = (int) event.getX();
y = (int) event.getY();
oldval = x+y;
break;
case MotionEvent.ACTION_MOVE:
{
Log.e("X value", "X is "+x);
Log.e("Y value", "Y is "+y);
try
{
Thread.sleep(500);
} catch (InterruptedException e) {
}
int newval= (int) (event.getX() + event.getY());
if(Math.abs(oldval-newval)>50)
{
Log.e("First", "next button");
longpressCount++;
if(longpressCount==1)
{
Log.e("1", "BUTTON PRESSED");
}
else if(longpressCount==2)
{
Log.e("2", "BUTTON PRESSED");
}
else if(longpressCount==3)
{
Log.e("3", "BUTTON PRESSED");
}
else if(longpressCount==4)
{
Log.e("4", "BUTTON PRESSED");
longpressCount = 0;
}
oldval = event.getX() + event.getY();
}
break;
}
}
return true;
}