MotionEvent初始化?

时间:2012-10-16 14:34:59

标签: java android ontouchevent

我正在尝试从另一个类调用onTouch方法,但我不知道要为MotionEvent传递什么。使用null会使应用程序强制关闭。继承人的代码

public final class Touch {

private static final String TAG = Touch.class.getSimpleName();

public boolean onTouchEvent(MotionEvent event, Droid droid, Thread thread) {
if (event.getAction() == MotionEvent.ACTION_DOWN) 
{
    droid.handleActionDown((int)event.getX(), (int)event.getY());
    /*if (event.getY() > getHeight() - 50) 
    {
        thread.setRunning(false);
        ((Activity)getContext()).finish();
    } 
    else 
    {
        Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
    }*/
} 
if (event.getAction() == MotionEvent.ACTION_MOVE) 
{
    // the gestures
    if (droid.isTouched()) 
    {
        // the droid was picked up and is being dragged
        droid.setX((int)event.getX());
        droid.setY((int)event.getY());
    }
} 
if (event.getAction() == MotionEvent.ACTION_UP) 
{
    // touch was released
    if (droid.isTouched()) 
    {
        droid.setTouched(false);
    }
}
return true;
}
}

这个类,我怎么在主线程中调用它?目前我正在尝试:

public void Touch()
{
    MotionEvent event = null;
    touch.onTouchEvent(event, droid, thread);
}

因为null是我能想到的唯一内容。

1 个答案:

答案 0 :(得分:0)

尝试:

MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);