2013-03-01 49 views
0

我想在我的NativeActivity类中使用GestureDetector,但它似乎不起作用。是否可以在NativeActivity应用程序中使用GestureDetector?我的代码是这样的:NativeActivity内部的手势检测器

public class HollywoodActivity extends android.app.NativeActivity { 

    private GestureDetector mGestureDetector; 
    private View.OnTouchListener mGestureListener; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 

      mGestureDetector = new GestureDetector(new MyGestureDetector()); 
      mGestureListener = new View.OnTouchListener() { 

       public boolean onTouch(View v, MotionEvent event) { 
        Log.i("DEBUG", "*** TOUCH VIEW ***"); 
        return mGestureDetector.onTouchEvent(event); 
       } 
      };         
    } 

    private class MyGestureDetector extends SimpleOnGestureListener { 

      @Override 
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

       Log.i("DEBUG", "FLING" + velocityX + " " + velocityY); 
       return true; 
      } 

      @Override 
      public boolean onDown(MotionEvent e) { 
       Log.i("DEBUG", "ON DOWN"); 
       return true; 
      }   
    } 
} 

当我运行我的应用程序,无论是onTouch(),也不onFling()也不onDown()曾经被调用。我在这里丢失了一些明显的东西,还是不可能使用GestureDetectorNativeActivity

感谢

编辑: 因为它似乎并不可能 它们传递到C端之前拦截在Java端MotionEvents,现在我已经尝试了另一种方式圆:每当我 得到C面的AINPUT_EVENT_TYPE_MOTION,我通过JNI喂养这在Java端的GestureDetector 这样的:

(*env)->CallVoidMethod(env, g_android->activity->clazz, (*env)->GetMethodID(env, globalMyNativeActivityClass, "runGestureDetector", "(JJIIFFFFIFFII)V"), AMotionEvent_getDownTime(event), AMotionEvent_getEventTime(event), AMotionEvent_getAction(event), AMotionEvent_getPointerCount(event), AMotionEvent_getRawX(event, 0), AMotionEvent_getRawY(event, 0), AMotionEvent_getPressure(event, 0), AMotionEvent_getSize(event, 0), AMotionEvent_getMetaState(event), AMotionEvent_getXPrecision(event), AMotionEvent_getYPrecision(event), AInputEvent_getDeviceId(event), AMotionEvent_getEdgeFlags(event)); 

Java方法runGestureDetector()简单地d Oes this then:

public void runGestureDetector(long downTime, long eventTime, int action, int pointerCount, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 

    mGestureDetector.onTouchEvent(MotionEvent.obtain(downTime, eventTime, action, pointerCount, x, y, pressure, size, metaState, xPrecision, yPrecision, deviceId, edgeFlags));    
} 

通过这样做,我能够检测到一些手势。 onDown(),onSingleTap()和onScroll() 进来就好了。然而,什么是不工作的onFling()。但是onFling()对我来说是最重要的一个。

我suspection是onFling()不起作用,因为它可能会依赖于可以存储在一个MotionEvent历史值 (每MotionEvent与历史 参数的历史尺寸,见的Android/input.h)。但是,MotionEvent类的gets()构造函数确实不允许我构造具有历史值的MotionEvent对象,它不允许我使用 。

那么有什么办法可以将我在C端获得的真实MotionEvent提供给Java端的 上的onTouchEvent()吗?我可以通过NDK API获得历史价值,但AFAICS是不可能 构建MotionEvent与Java方面的历史信息:-(

任何想法?

回答

0

我没有测试这一点,我不熟悉NativeActivity但据我所看到的,你不钩住你的GestureDetector
创建OnTouchListener相反的,尝试去驾驭你的活动的onTouchEvent(MotionEvent event)

@Override 
public boolean onTouchEvent (MotionEvent event) { 
    Log.i("DEBUG", "*** TOUCH VIEW ***"); 
    return mGestureDetector.onTouchEvent(event); 
} 

这会将您Activity上的每个MotionEvent传递给您的GestureDetector。如果你想捉对特定视图仅MotionEvents您可以将OnTouchListener重视它:

findViewById(R.id.yourView).setOnTouchListener(mGestureListener); 

就像我已经说过了,这可能仅适用于正常活动,我不知道如何NativeActivities工作...

无论如何,我希望这有助于。

+0

感谢您的回复。我应该提到我已经尝试覆盖NativeActivity的onTouchEvent()。尽管Android文档说NativeActivity从Activity类继承了onTouchEvent()方法,但它永远不会被调用。所以不幸的是,这是不行的:/ – Andreas 2013-03-01 20:51:16

+0

对此我很抱歉。我也只会评论你的问题,但我的声誉还不允许。不幸的是,我不熟悉C/C++,但是你看看[onInputQueueCreated](http://developer.android.com/reference/android/app/NativeActivity.html#onInputQueueCreated(android.view.InputQueue ))? – Griddo 2013-03-01 21:10:19

+0

嗯,你认为可以在onInputQueueCreated()帮助这里?我不知何故需要找到一种方法来拦截所有的触摸事件,将它们转发给GestureDetector。我没有看到onInputQueueCreated()如何在这里帮助... – Andreas 2013-03-01 21:20:16