2015-03-13 26 views

回答

2

由於@Daniel建議,我創建MotionEvents的定製版本,因爲使用多個手指時,你必須注入ACTION_POINTER_DOWN/UP,而不是ACTION_DOWN/UP。 我創建LinearSwipe的twoFinger版本,像這樣:

private static Swiper.Status sendLinearSwipe(UiController uiController, 
              float[] startCoordinates, 
              float[] startCoordinatesSecondFinger, 
              float[] endCoordinates, 
              float[]endCoordinatesSecondFinger, 
              float[] precision, 
              float[] precisionSecond, 
              int duration) { 
    checkNotNull(uiController); 
    checkNotNull(startCoordinates); 
    checkNotNull(startCoordinatesSecondFinger); 
    checkNotNull(endCoordinates); 
    checkNotNull(endCoordinatesSecondFinger); 
    checkNotNull(precision); 
    checkNotNull(precisionSecond); 

    float[][] steps = interpolate(startCoordinates, endCoordinates, SWIPE_EVENT_COUNT); 
    float[][] stepsSecondFinger = interpolate(startCoordinatesSecondFinger, endCoordinatesSecondFinger, SWIPE_EVENT_COUNT); 
    final int delayBetweenMovements = duration/steps.length; 
    final int delayBetweenMovementsSecondFinger = duration/stepsSecondFinger.length; 

    int maxLength=Math.min(steps.length, stepsSecondFinger.length); 

    MotionEvent downEvent; 
    downEvent = MotionEvents.sendDown(uiController, steps[0], precision,true).down; 
    MotionEvent downEventSecondFinger; 
    downEventSecondFinger = MotionEvents.sendDown(uiController,stepsSecondFinger[0], precisionSecond,false).down; 

    try { 
     for (int i = 1; i < maxLength; i++) { 

      if (sendMovement(uiController, steps[i], downEvent)) return Status.FAILURE; 
      if (sendMovement(uiController, stepsSecondFinger[i], downEventSecondFinger)) return Status.FAILURE; 


      long desiredTime = downEvent.getDownTime() + delayBetweenMovements * i; 
      long desiredTimeSecondFinger = downEventSecondFinger.getDownTime() + delayBetweenMovementsSecondFinger * i; 

      long timeUntilDesired = desiredTime - SystemClock.uptimeMillis(); 
      long timeUntilDesiredSecondFinger = desiredTimeSecondFinger - SystemClock.uptimeMillis(); 
      loopMainThread(uiController, timeUntilDesired); 
      loopMainThread(uiController, timeUntilDesiredSecondFinger); 

     } 

     if (!MotionEvents.sendUp(uiController, downEventSecondFinger, endCoordinatesSecondFinger,false)) { 
      Log.e(TAG, "Injection of up event as part of the swipe failed. Sending cancel event."); 
      MotionEvents.sendCancel(uiController, downEventSecondFinger); 
      return Swiper.Status.FAILURE; 
     } 
    } finally { 
     downEvent.recycle(); 
     downEventSecondFinger.recycle(); 
    } 
    return Swiper.Status.SUCCESS; 
} 

private static void loopMainThread(UiController uiController, long timeUntilDesired) { 
    if (timeUntilDesired > 10) { 
     uiController.loopMainThreadForAtLeast(timeUntilDesired); 
    } 
} 

private static boolean sendMovement(UiController uiController, float[] step, MotionEvent downEvent) { 
    if (!MotionEvents.sendMovement(uiController, downEvent, step)) { 
     Log.e(TAG, "Injection of move event as part of the swipe failed. Sending cancel event."); 
     MotionEvents.sendCancel(uiController, downEvent); 
     return true; 
    } 
    return false; 
} 

現在,它完美的作品!由於@Daniel

+1

您是否還可以在您的示例中包含interpolate方法? – Adrian 2016-03-23 10:03:45

1

咖啡不提供該功能,但你可以通過

  • 注射兩降事件
  • 注入幾雙運動事件,每一個手指
  • 注入兩個上事件做自己

意式濃縮咖啡有一些實用工具可以使它更容易。特別是,MotionEvents類有一些輔助方法來創建和注入這些低級事件。

您可能要參考sendLinearSwipe代碼包含大部分邏輯的單點觸摸刷卡。

如果您將其編寫爲ViewAction,它將完全適合Espresso框架(例如,您可以輕鬆地等待空閒資源)。

+0

謝謝,我現在的工作。但是,當我注入兩個事件時它失敗了。但是,如果我只注入一個事件,它可以正常工作:) – HowieH 2015-03-18 14:16:57