【问题标题】:How to programmatically trigger the touch event in android?如何以编程方式触发android中的触摸事件?
【发布时间】:2014-07-17 03:52:37
【问题描述】:

我想触发这样的触摸事件:

首先手指在屏幕的 (0,50%) 处触碰并滑动到屏幕的 (50%,50%),然后退出(将手指从屏幕上移开)

我发现了一些类似的东西:

MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, pressure, size, metaState, xPrecision, yPrecision, deviceId, edgeFlags);

onTouchEvent(event);

但是,如何模拟上述情况?我需要创建 2 个事件吗? onTouchDown , onMove 等....?感谢您的帮助。

【问题讨论】:

  • 现在我已经有了 ontouchEvent 的功能,我想以编程方式触发它,而不是用户手动触摸屏幕
  • 我曾想过直接在函数中调用 ontouch 事件,但它相当复杂,所以我认为最好的方法是模拟触摸事件

标签: android touch gesture-recognition gestures


【解决方案1】:
// Obtain MotionEvent object
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 0.0f;
float y = 0.0f;
// List of meta states found here:     developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
    downTime, 
    eventTime, 
    MotionEvent.ACTION_UP, 
    x, 
    y, 
    metaState
);

// Dispatch touch event to view
view.dispatchTouchEvent(motionEvent);

【讨论】:

  • 应该如何声明和初始化视图对象?
  • 向左、向右、顶部和底部滑动手势怎么样。使用 ACTION_MOVE 是否可能?
  • @Ramesh_D 你必须生成很多 ACTION_MOVE 事件来模拟滑动。
  • 可以使用ACTION_DOWNACTION_MOVEACTION_UP 生成滑动。
  • 我用这个,效果很好,调用了 onTouch 事件,但是当我使用上面的代码并将手指放在视图上并移动时,我发现了一个问题,ontouchevent 没有被调用
【解决方案2】:

这是干净的版本:

public void TouchView(View view)
{
    view.DispatchTouchEvent(MotionEvent.Obtain(SystemClock.UptimeMillis(), SystemClock.UptimeMillis(), (int)MotionEventActions.Down, 0, 0, 0));
    view.DispatchTouchEvent(MotionEvent.Obtain(SystemClock.UptimeMillis(), SystemClock.UptimeMillis(), (int)MotionEventActions.Up, 0, 0, 0));
}

PS:这是一个 xamarin android 解决方案,但您可以轻松地为 java 修改它

【讨论】:

    【解决方案3】:

    添加上面@bstar55 的出色答案 - 如果您想要创建一个“点击”事件,例如,用户点击视图,那么您需要模拟用户触摸屏幕然后移除他们的在很短的时间内手指。

    为此,您“调度”一个 ACTION_DOWN MotionEvent,然后在短时间内执行一个 ACTION_UP MotionEvent。

    以下示例在 Kotlin 中经过测试并可靠运行:

       //Create amd send a tap event at the current target loctaion to the PhotoView
       //From testing (on an original Google Pixel) a tap event needs an ACTION_DOWN followed shortly afterwards by
       //an ACTION_UP, so send both events
       //First, create amd send the ACTION_DOWN MotionEvent
       var originalDownTime: Long = SystemClock.uptimeMillis()
       var eventTime: Long = SystemClock.uptimeMillis() + 100
       var x = your_X_Value
       var y = your_Y_Value 
       var metaState = 0
       var motionEvent = MotionEvent.obtain(
               originalDownTime,
               eventTime,
               MotionEvent.ACTION_DOWN,
               x,
               y,
               metaState
        )
        var returnVal = yourView.dispatchTouchEvent(motionEvent)
        Log.d(TAG,"rteurnVal: " + returnVal)
    
        //Create amd send the ACTION_UP MotionEvent
        eventTime= SystemClock.uptimeMillis() + 100
        motionEvent = MotionEvent.obtain(
               originalDownTime,
               eventTime,
               MotionEvent.ACTION_UP,
               x,
               y,
               metaState
         )
         returnVal = yourView.dispatchTouchEvent(motionEvent)
         Log.d(TAG,"rteurnVal: " + returnVal)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-01
      • 2012-10-29
      • 2017-05-22
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2015-07-21
      • 2017-03-02
      相关资源
      最近更新 更多