我有一个需要onTouch滑动事件的应用程序,如Iphone。他们是
我实现了onTouch事件,如下所示。我正确地向左和向右滑动操作。但这是实现向下和向上滑动操作的正确方法。
mycode的:
float downXValue,downYValue;
@Override
public boolean onTouchEvent(MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
downYValue = arg1.getY();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
float currentY=arg1.getY();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
Log.d(DEBUG_TAG, "Right");
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
Log.d(DEBUG_TAG, "Left");
}
break;
}
}
//GestureListener is called here...
//return gestures.onTouchEvent(event);
return true;
}
谢谢。
答案 0 :(得分:3)
怎么样......
if (downYValue < currentY)
{
Log.d(DEBUG_TAG, "Down");
}
您确定在上面编写了代码吗?
修改强>
好的,我相信你。你要做的基本上是:double sizeInX = Math.abs(downXValue - currentX);
double sizeInY = Math.abs(downYValue - currentY);
if( sizeInX > sizeInY ){
// you better swipe horizontally
} else {
// you better swipe vertically
}
答案 1 :(得分:2)
这是我前段时间写过的一个监听器,它可以处理两个向下的swype。我用来跟踪触摸位置的float oldPosition
变量。那里有一些不需要的代码,但你会明白这个想法:
LinearLayout tableM = (LinearLayout) findViewById (R.id.tableM);
tableM.setOnTouchListener(new OnTouchListener () {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
oldPosition = event.getY();
return true;
} else
if (event.getAction() == android.view.MotionEvent.ACTION_MOVE) {
if (event.getY() < oldPosition) {
tableIndexM++;
if (tableIndexM > (minsecs.length - 3)) {
tableIndexM = 0;
}
fillTable (rowM0, rowM1, rowM2, minsecs, tableIndexM);
}
else if (event.getY() > oldPosition) {
tableIndexM--;
if (tableIndexM < 0) {
tableIndexM = minsecs.length - 3;
}
fillTable (rowM0, rowM1, rowM2, minsecs, tableIndexM);
}
oldPosition = event.getY();
return true;
}
return false;
}
});