4

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

我尝试的是在视图的 onTouchListener 的 onTouch 方法中处理 ACTION_UP 事件,但是这种方式 onFLing 不起作用。我还在这里阅读了一个解决方案:GestureDetector 上的 OnUp 事件,它说我应该覆盖 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;
     }

}

}

4

1 回答 1

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;
}
于 2013-01-08T14:12:24.067 回答