我已经问过这个问题,但当时我认为onTouch事件之间的刷新时间是我的问题Android onTouch refresh rate。但事实并非如此。所以我的问题再一次: 当我的手指触摸显示屏时,我需要一个Sprite向左/向右/向上移动。所以我的onTouchEvent返回true。但这只适用于我的手指滑动一下或者触摸屏驱动程序如此糟糕以至于它会因检测到手指移动而抖动的情况。 我用Threads解决了一个问题。所以我开始一个移动播放器的线程,当手指被抬起时它会被中断。但也许有更好的方法可以做到这一点?
@Override
public boolean onTouchEvent(MotionEvent me) {
...
if (me.getY() < getHeight() / 2) {
return doAction(0);
} else if (me.getX() >= getWidth() / 2)
return doAction(2);
else if (me.getX() < getWidth() / 2)
return doAction(1);
//doAction returns TRUE
...
}
答案 0 :(得分:1)
final Button b=(Button)findViewById(R.id.button1);
b.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
b.setBackgroundResource(R.drawable.ic_launcher);
finish();
return true;
case MotionEvent.ACTION_DOWN:
//work u want to do etc ..use all the events like this
}
b.setBackgroundResource(R.drawable.ic_back);
return false;
}
});
答案 1 :(得分:1)
好吧我用Threads管理它。这是doAction的一部分。
if (event == MotionEvent.ACTION_DOWN) {
movePlayer = new Thread() {
public void run() {
while (!this.isInterrupted()) {
if (main.player.x > 5
&& now.getLevel()[playerXsc][playerYsc] == Level.FLAG_SKY
&& now.getLevel()[playerXsc][playerY2sc] == Level.FLAG_SKY) {
main.player.x -= 4;
} else if (main.player.x <= 5 && now instanceof L9)
main.player.x = aspectX * 19;
try {
Thread.sleep(5);
} catch (InterruptedException e) { break; }
}
}
};
movePlayer.start();
} else if (event == MotionEvent.ACTION_UP) {
Log.e("ui", "mpt interrupt");
movePlayer.interrupt();
}
答案 2 :(得分:0)
如果您的doAction方法非常重要,那么这将导致延迟,因为未来的触摸事件将“缓冲”等待来自此onTouchEvent处理程序的响应。另外,你在使用
runOnUiThread(new Runnable() {
public void run() {
// Changes to UI here...
}
});
答案 3 :(得分:0)
WebView1 = (WebView) findViewById(R.id.StudyWebview);
detector = new GestureDetector(this, this);
WebView1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
请实现OnGestureListener并使用其回调方法
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float arg3) {
float dX = e2.getX() - e1.getX();
float dY = e1.getY() - e2.getY();
// check is all completed or return with some condition
if (Math.abs(dY) < SWIPE_MAX_OFF_PATH && Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY && Math.abs(dX) >= SWIPE_MIN_DISTANCE) {
// logic for left and right
if (dX < 0) {
}
else if (dX > 0) {
}
}
return true;
}
@Override
public void onLongPress(MotionEvent arg0) {
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}