react-native-navigation的迁移库

OverlayTouchDelegate.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.reactnativenavigation.views.touch;
  2. import android.graphics.Rect;
  3. import android.support.annotation.VisibleForTesting;
  4. import android.view.MotionEvent;
  5. import android.view.ViewGroup;
  6. import com.reactnativenavigation.parse.params.Bool;
  7. import com.reactnativenavigation.parse.params.NullBool;
  8. import com.reactnativenavigation.utils.UiUtils;
  9. import com.reactnativenavigation.viewcontrollers.IReactView;
  10. public class OverlayTouchDelegate {
  11. private enum TouchLocation {Outside, Inside}
  12. private final Rect hitRect = new Rect();
  13. private IReactView reactView;
  14. private Bool interceptTouchOutside = new NullBool();
  15. public OverlayTouchDelegate(IReactView reactView) {
  16. this.reactView = reactView;
  17. }
  18. public boolean onInterceptTouchEvent(MotionEvent event) {
  19. if (interceptTouchOutside instanceof NullBool) return false;
  20. switch (event.getActionMasked()) {
  21. case MotionEvent.ACTION_DOWN:
  22. return handleDown(event);
  23. default:
  24. reactView.dispatchTouchEventToJs(event);
  25. return false;
  26. }
  27. }
  28. @VisibleForTesting
  29. public boolean handleDown(MotionEvent event) {
  30. TouchLocation location = getTouchLocation(event);
  31. if (location == TouchLocation.Inside) {
  32. reactView.dispatchTouchEventToJs(event);
  33. }
  34. if (interceptTouchOutside.isTrue()) {
  35. return location == TouchLocation.Inside;
  36. }
  37. return location == TouchLocation.Outside;
  38. }
  39. private TouchLocation getTouchLocation(MotionEvent ev) {
  40. ((ViewGroup) reactView.asView()).getChildAt(0).getHitRect(hitRect);
  41. return hitRect.contains((int) ev.getRawX(), (int) ev.getRawY() - UiUtils.getStatusBarHeight(reactView.asView().getContext())) ?
  42. TouchLocation.Inside :
  43. TouchLocation.Outside;
  44. }
  45. public void setInterceptTouchOutside(Bool interceptTouchOutside) {
  46. this.interceptTouchOutside = interceptTouchOutside;
  47. }
  48. }