使用SimpleOnGestureListener实现onUp事件处理程序

时间:2013-01-08 13:32:58

标签: android

我对Android编程很陌生,最近我把注意力转向处理简单的手势。我知道GestureDetector和监听器,我成功地实现了我需要的简单手势(onDown,onFling,onScroll)。 问题是我需要在SimpleOnGestureListener类中不可用的onUp方法。 我有一个活动和一个自定义视图。视图并没有真正做任何事情,只是改变了背景颜色。我正在活动中进行所有事件处理。

我尝试的是在视图的onTouchListener的onTouch方法中处理ACTION_UP事件,但这种方式onFLing将不起作用。 我还在这里阅读了一个解决方案:OnUp event on GestureDetector,它说我应该覆盖onTouchEvent,但我不清楚我应该覆盖哪个onTouchEvent,在哪里以及如何覆盖。 我正在插入我当前的代码,也许它有所帮助。 :)

public class Main extends Activity 
{
private GestureDetector gDetector;
public CustomView cView;
View.OnTouchListener gestureListener;

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

public Vibrator vibrator;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
    gDetector=new GestureDetector(this, new gListener());
    gDetector.setIsLongpressEnabled(true);
    gestureListener=new View.OnTouchListener(){

        public boolean onTouch(View v, MotionEvent event) 
        {
            if(event.getAction()==MotionEvent.ACTION_UP)
            {
                cView.setBackgroundColor(Color.RED);
                return true;
            }
            else
            return gDetector.onTouchEvent(event);
        }
    };
    cView=new CustomView(this);
    cView.setOnTouchListener(gestureListener);
    cView.setBackgroundColor(Color.BLACK);
    cView.setLongClickable(true);
    setContentView(cView);
}

class gListener extends GestureDetector.SimpleOnGestureListener
{

     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
     {
         try 
         {
             if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                 return false;
             if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 cView.setBackgroundColor(Color.WHITE);
             }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 cView.setBackgroundColor(Color.GRAY);
             }
         } catch (Exception e) {
             // nothing
         }
         return false;
     }

     @Override
     public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
     {
         vibrator.vibrate(30);
         Log.d("Event", "MOVE");
         return true;
     }

     @Override
     public boolean onDown(MotionEvent e)
     {
         cView.setBackgroundColor(Color.BLUE);
         Log.d("Event", "DOWN");
         return true;
     }

     @Override
     public boolean onSingleTapUp(MotionEvent e)
     {
         Log.d("Event", "UP"); 
         return true;
     }

}

}

1 个答案:

答案 0 :(得分:4)

尝试这种方法......

@Override
public boolean onTouchEvent(MotionEvent me) {

Log.v("Touch", "ontouch: " + me.getAction());

if(me.getAction() == 0){
    Log.v("Touch Down", "Down");
}
else if (me.getAction() == 1) {
    Log.v("Touch Up", "Up");
}
else if (me.getAction() == 2) {
    Log.v("touch Scroll", "Scroll");
}
boolean detectedUp = event.getAction() == MotionEvent.ACTION_UP;
if (!mGestureDetector.onTouchEvent(event) && detectedUp)
{
    return onUp(event);
}
return true;
}