Android Touch 이벤트 배포 과정 상세 설명

29892 단어 AndroidTouch
본 고 는 안 드 로 이 드 터치 사건 의 배포 과정 을 사례 로 서술 하여 안 드 로 이 드 프로 그래 밍 을 깊이 이해 하고 파악 하 는 데 큰 도움 이 된다.구체 적 인 분석 은 다음 과 같다.
우선,간단 한 예제 에서 시작 합 니 다.
다음 그림 과 같은 예 시 를 먼저 보 세 요.

레이아웃 파일:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:id="@+id/container" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:layout_gravity="center" 
  tools:context="com.example.touch_event.MainActivity" 
  tools:ignore="MergeRootFrame" > 
 
  <Button 
    android:id="@+id/my_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 
 
</FrameLayout> 

MainActivity 파일:

public class MainActivity extends Activity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
 
    Button mBtn = (Button) findViewById(R.id.my_button); 
    mBtn.setOnTouchListener(new OnTouchListener() { 
 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
        Log.d("", "### onTouch : " + event.getAction()); 
        return false; 
      } 
    }); 
    mBtn.setOnClickListener(new OnClickListener() { 
 
      @Override 
      public void onClick(View v) { 
        Log.d("", "### onClick : " + v); 
      } 
    }); 
 
  } 
 
  @Override 
  public boolean dispatchTouchEvent(MotionEvent ev) { 
    Log.d("", "### activity dispatchTouchEvent"); 
    return super.dispatchTouchEvent(ev); 
  } 
} 

사용자 가 단 추 를 누 르 면 다음 로그 가 출력 됩 니 다:

08-31 03:03:56.116: D/(1560): ### activity dispatchTouchEvent 
08-31 03:03:56.116: D/(1560): ### onTouch : 0 
08-31 03:03:56.196: D/(1560): ### activity dispatchTouchEvent 
08-31 03:03:56.196: D/(1560): ### onTouch : 1 
08-31 03:03:56.196: D/(1560): ### onClick : android.widget.Button{52860d98 VFED..C. ...PH... 0,0-1080,144 #7f05003d app:id/my_button} 

Activity 의 dispatchTouchEvent 방법 을 먼저 실행 한 다음 에 onTouch 방법 을 실행 한 다음 에 dispatchTouchEvent-->onTouch,마지막 으로 실행 버튼 의 클릭 이 벤트 를 볼 수 있 습 니 다.여기 서 우 리 는 왜 dispatchTouchEvent 와 onTouch 가 두 번 이나 실 행 했 는 지 의문 이 있 을 수 있 습 니 다.onClick 은 한 번 밖 에 실 행 했 습 니까?왜 두 번 의 Touch 사건 의 action 이 다 릅 니까?action 0 과 action 1 은 도대체 무엇 을 대표 합 니까?
onTouch Event 를 복사 한 친 구 는 일반적으로 우리 가 이 방법 에서 집중 touch 유형의 사건 을 처리 하 는 것 을 알 고 있 습 니 다.ACTION 이 있 습 니 다.DOWN、ACTION_MOVE、ACTION_UP 등,그러나 위의 우리 의 예 에서 이동 하지 않 고 단순히 누 르 고 들 었 을 뿐이다.따라서 우리 의 터치 사건 도 누 르 고 들 어야 하기 때문에 터치 사건 이 2 번 있 고 action 은 각각 0 과 1 이다.모 션 이벤트 의 변수 정 의 를 살 펴 보 자.

public final class MotionEvent extends InputEvent implements Parcelable { 
//      
  public static final int ACTION_DOWN       = 0;  //      
  public static final int ACTION_UP        = 1;  //       
  public static final int ACTION_MOVE       = 2;  //        
  public static final int ACTION_CANCEL      = 3;  //    
 //      
} 

대표 가 누 른 사건 이 0 이 고 사건 을 1 로 들 어 올 리 는 것 도 우리 가 위 에서 말 한 것 을 입증 한 것 으로 보인다.
다른 두 장면 을 보고 있 습 니 다.
1.우 리 는 버튼 밖의 영역 을 클릭 하고 Log 를 다음 과 같이 출력 합 니 다.

08-31 03:04:45.408: D/(1560): ### activity dispatchTouchEvent08-31  
03:04:45.512: D/(1560): ### activity dispatchTouchEvent 

2.우 리 는 onTouch 함수 에서 true 를 되 돌려 줍 니 다.출력 Log 는 다음 과 같 습 니 다.

08-31 03:06:04.764: D/(1612): ### activity dispatchTouchEvent 
08-31 03:06:04.764: D/(1612): ### onTouch : 0 
08-31 03:06:04.868: D/(1612): ### activity dispatchTouchEvent 
08-31 03:06:04.868: D/(1612): ### onTouch : 1 

이상 두 장면 은 왜 이 럴 까요?   우리 계속 내 려 다 보 자.
Android Touch 이벤트 배포
그렇다면 전체 사건 배포 절 차 는 어떻게 될 까? 
쉽게 말 하면 사용자 가 핸드폰 화면 을 터치 하면 터치 메시지 가 생 긴 다 는 것 이다.최종 적 으로 이 터치 메 시 지 는 ViewRoot(4.2 의 소스 코드 를 볼 때 이 종 류 는 ViewRootImpl)의 InputHandler 로 전송 된다.ViewRoot 는 GUI 관리 시스템 과 GUI 가 시스템 간 의 다 리 를 보 여 주 는 것 이다.ViewRoot 의 정의 에 따 르 면 이것 은 View 유형 이 아니 라 Handler 라 는 것 을 알 수 있다.InputHandler 는 키 이벤트 와 터치 이벤트 형식의 이 벤트 를 처리 하 는 인터페이스 형식 입 니 다.원본 코드 를 보십시오.

public final class ViewRoot extends Handler implements ViewParent, 
    View.AttachInfo.Callbacks { 
      //      
  private final InputHandler mInputHandler = new InputHandler() { 
    public void handleKey(KeyEvent event, Runnable finishedCallback) { 
      startInputEvent(finishedCallback); 
      dispatchKey(event, true); 
    } 
    public void handleMotion(MotionEvent event, Runnable finishedCallback) { 
      startInputEvent(finishedCallback); 
      dispatchMotion(event, true);   // 1、handle      
    } 
  }; 
    //      
  // 2、       
  private void dispatchMotion(MotionEvent event, boolean sendDone) { 
    int source = event.getSource(); 
    if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) { 
      dispatchPointer(event, sendDone);   //        
    } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) { 
      dispatchTrackball(event, sendDone); 
    } else { 
      // TODO 
      Log.v(TAG, "Dropping unsupported motion event (unimplemented): " + event); 
      if (sendDone) { 
        finishInputEvent(); 
      } 
    } 
  } 
  // 3、  Handler     
  private void dispatchPointer(MotionEvent event, boolean sendDone) { 
    Message msg = obtainMessage(DISPATCH_POINTER); 
    msg.obj = event; 
    msg.arg1 = sendDone ? 1 : 0; 
    sendMessageAtTime(msg, event.getEventTime()); 
  } 
  @Override 
  public void handleMessage(Message msg) {      // ViewRoot  handlerMessage        
    switch (msg.what) { 
      //      
    case DO_TRAVERSAL: 
      if (mProfile) { 
        Debug.startMethodTracing("ViewRoot"); 
      } 
 
      performTraversals(); 
 
      if (mProfile) { 
        Debug.stopMethodTracing(); 
        mProfile = false; 
      } 
      break; 
 
    case DISPATCH_POINTER: {    // 4、  DISPATCH_POINTER     ,         
      MotionEvent event = (MotionEvent) msg.obj; 
      try { 
        deliverPointerEvent(event); // 5、       
      } finally { 
        event.recycle(); 
        if (msg.arg1 != 0) { 
          finishInputEvent(); 
        } 
        if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!"); 
      } 
    } break; 
    //      
  } 
  // 6、        
  private void deliverPointerEvent(MotionEvent event) { 
    if (mTranslator != null) { 
      mTranslator.translateEventInScreenToAppWindow(event); 
    } 
    boolean handled; 
    if (mView != null && mAdded) { 
      // enter touch mode on the down 
      boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN; 
      if (isDown) { 
        ensureTouchMode(true);  //    ACTION_DOWN         ,       。 
      } 
      if(Config.LOGV) { 
        captureMotionLog("captureDispatchPointer", event); 
      } 
      if (mCurScrollY != 0) { 
        event.offsetLocation(0, mCurScrollY);  //              
      } 
      if (MEASURE_LATENCY) { 
        lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano()); 
      } 
      // 7、    ,       ,    mView     PhonwWindow  DecorView,       ViewGroup。 
      handled = mView.dispatchTouchEvent(event); 
      //        
    } 
  } 
  //      
}  
코드 7 곳 의 mView 가 DecorView 든 비 창 인터페이스의 루트 보기 든 그 본질은 ViewGroup 입 니 다.즉,터치 사건 은 루트 보기 ViewGroup 에 의 해 배 포 됩 니 다!!
        우 리 는 Activity 를 예 로 들 어 이 과정 을 분석 합 니 다.우 리 는 보 여 준 Activity 에 꼭대기 층 창 이 있다 는 것 을 알 고 있 습 니 다.이 창의 실현 류 는 PhoneWindow 이 고 PhoneWindow 의 내용 구역 은 DecorView 유형의 View 입 니 다.이 View 는 바로 우리 가 핸드폰 에서 본 내용 입 니 다.이 DecorView 는 FrameLayout 의 하위 클래스 입 니 다.Activity 의 dispatchTouchEvent 는 실제 적 으로 PhoneWindow 의 dispatchTouchEvent 를 호출 하 는 것 입 니 다.소스 코드 를 보 겠 습 니 다.Activity 의 dispatchTouchEvent 함수 에 들 어가 십시오.

public boolean dispatchTouchEvent(MotionEvent ev) { 
   if (ev.getAction() == MotionEvent.ACTION_DOWN) { 
     onUserInteraction(); 
   } 
   if (getWindow().superDispatchTouchEvent(ev)) {   // 1、    PhoneWindow superDispatchTouchEvent(ev) 
 
     return true; 
   } 
   return onTouchEvent(ev); 
 } 
 
 public void onUserInteraction() { 
 } 

이벤트 가 이 벤트 를 누 르 기 위해 서 라면 onUserInteraction()함수 에 들 어 갑 니 다.이 함 수 는 비어 있 습 니 다.우 리 는 잠시 그것 을 상관 하지 않 습 니 다.계속 보 니 터치 이벤트 의 배포 가 getWindow().슈퍼 DispatchTouchEvent(ev)함수,getWindow()에서 가 져 온 인 스 턴 스 의 종 류 는 PhoneWindow 형식 입 니 다.Activity 클래스 에서 getWindow()에서 가 져 온 종 류 를 다음 과 같이 볼 수 있 습 니 다.

Log.d("", "### Activiti getWindow()       : " + this.getWindow()) ; 

출력:

08-31 03:40:17.036: D/(1688): ### Activiti getWindow()       : com.android.internal.policy.impl.PhoneWindow@5287fe38 

OK,쓸데없는 소리 하지 말고 Phone Window 의 슈퍼 디 스 패 치 터치 이벤트 함 수 를 계속 봅 시다.

@Override 
public boolean superDispatchTouchEvent(MotionEvent event) { 
  return mDecor.superDispatchTouchEvent(event); 
} 

네,mDecor 의 슈퍼 DispatchTouchEvent(event)함 수 를 호출 했 습 니 다.이 mDecor 는 바로 우리 가 위 에서 말 한 DecorView 유형 입 니 다.즉,우리 가 본 Activity 의 모든 내용 의 최상 위 ViewGroup,즉 전체 ViewTree 의 뿌리 노드 입 니 다.그것 의 성명 을 보 세 요.

// This is the top-level view of the window, containing the window decor. 
private DecorView mDecor; 
DecorView
그럼 DecorView 가 어떤 물건 인지 계속 볼 게 요.

  private final class DecorView extends FrameLayout implements RootViewSurfaceTaker { 
    /* package */int mDefaultOpacity = PixelFormat.OPAQUE; 
 
    /** The feature ID of the panel, or -1 if this is the application's DecorView */ 
    private final int mFeatureId; 
 
    private final Rect mDrawingBounds = new Rect(); 
 
    private final Rect mBackgroundPadding = new Rect(); 
 
    private final Rect mFramePadding = new Rect(); 
 
    private final Rect mFrameOffsets = new Rect(); 
 
    private boolean mChanging; 
 
    private Drawable mMenuBackground; 
    private boolean mWatchingForMenu; 
    private int mDownY; 
 
    public DecorView(Context context, int featureId) { 
      super(context); 
      mFeatureId = featureId; 
    } 
 
    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) { 
      final int keyCode = event.getKeyCode(); 
      //      
      return isDown ? PhoneWindow.this.onKeyDown(mFeatureId, event.getKeyCode(), event) 
          : PhoneWindow.this.onKeyUp(mFeatureId, event.getKeyCode(), event); 
    } 
 
    @Override 
    public boolean dispatchTouchEvent(MotionEvent ev) { 
      final Callback cb = getCallback(); 
      return cb != null && mFeatureId < 0 ? cb.dispatchTouchEvent(ev) : super 
          .dispatchTouchEvent(ev); 
    } 
 
    @Override 
    public boolean dispatchTrackballEvent(MotionEvent ev) { 
      final Callback cb = getCallback(); 
      return cb != null && mFeatureId < 0 ? cb.dispatchTrackballEvent(ev) : super 
          .dispatchTrackballEvent(ev); 
    } 
 
    public boolean superDispatchKeyEvent(KeyEvent event) { 
      return super.dispatchKeyEvent(event); 
    } 
 
    public boolean superDispatchTouchEvent(MotionEvent event) { 
      return super.dispatchTouchEvent(event); 
    } 
 
    public boolean superDispatchTrackballEvent(MotionEvent event) { 
      return super.dispatchTrackballEvent(event); 
    } 
 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
      return onInterceptTouchEvent(event); 
    } 
//      
} 

이 를 통 해 알 수 있 듯 이 DecorView 는 FrameLayout 에서 계승 되 었 습 니 다.이 는 touch 사건 의 배포(dispatchTouchEvent),처 리 는 모두 슈퍼 클래스 에 의 해 처리 되 었 습 니 다.즉,FrameLayout 에 의 해 처리 되 었 습 니 다.우 리 는 FrameLayout 에서 해당 하 는 실현 을 보지 못 했 습 니 다.그러면 FrameLayout 의 부모 클래스,즉 View Group 까지 계속 추적 하여 dispatchEvent 의 실현 을 보 았 습 니 다.그럼 뷰 그룹(Android 2.3 소스)이 이벤트 배 포 를 어떻게 진행 하 는 지 살 펴 보 자.
ViewGroup 의 Touch 이벤트 배포

/** 
 * {@inheritDoc} 
 */ 
@Override 
public boolean dispatchTouchEvent(MotionEvent ev) { 
  if (!onFilterTouchEventForSecurity(ev)) { 
    return false; 
  } 
 
  final int action = ev.getAction(); 
  final float xf = ev.getX(); 
  final float yf = ev.getY(); 
  final float scrolledXFloat = xf + mScrollX; 
  final float scrolledYFloat = yf + mScrollY; 
  final Rect frame = mTempRect; 
 
  boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; 
 
  if (action == MotionEvent.ACTION_DOWN) { 
    if (mMotionTarget != null) { 
      // this is weird, we got a pen down, but we thought it was 
      // already down! 
      // XXX: We should probably send an ACTION_UP to the current 
      // target. 
      mMotionTarget = null; 
    } 
    // If we're disallowing intercept or if we're allowing and we didn't 
    // intercept 
    if (disallowIntercept || !onInterceptTouchEvent(ev))     // 1、      、       
      // reset this event's action (just to protect ourselves) 
      ev.setAction(MotionEvent.ACTION_DOWN); 
      // We know we want to dispatch the event down, find a child 
      // who can handle it, start with the front-most child. 
      final int scrolledXInt = (int) scrolledXFloat; 
      final int scrolledYInt = (int) scrolledYFloat; 
      final View[] children = mChildren; 
      final int count = mChildrenCount; 
 
      for (int i = count - 1; i >= 0; i--)    // 2、     view,          view       
        final View child = children[i]; 
        if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE 
            || child.getAnimation() != null) { 
          child.getHitRect(frame);        // 3、  child      
          if (frame.contains(scrolledXInt, scrolledYInt))  // 4、             child      
            // offset the event to the view's coordinate system 
            final float xc = scrolledXFloat - child.mLeft; 
            final float yc = scrolledYFloat - child.mTop; 
            ev.setLocation(xc, yc); 
            child.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; 
            if (child.dispatchTouchEvent(ev))   // 5、child      
              // Event handled, we have a target now. 
              mMotionTarget = child; 
              return true; 
            } 
            // The event didn't get handled, try the next view. 
            // Don't reset the event's location, it's not 
            // necessary here. 
          } 
        } 
      } 
    } 
  } 
 
  boolean isUpOrCancel = (action == MotionEvent.ACTION_UP) || 
      (action == MotionEvent.ACTION_CANCEL); 
 
  if (isUpOrCancel) { 
    // Note, we've already copied the previous state to our local 
    // variable, so this takes effect on the next event 
    mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; 
  } 
 
  // The event wasn't an ACTION_DOWN, dispatch it to our target if 
  // we have one. 
  final View target = mMotionTarget; 
  if (target == null) { 
    // We don't have a target, this means we're handling the 
    // event as a regular view. 
    ev.setLocation(xf, yf); 
    if ((mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { 
      ev.setAction(MotionEvent.ACTION_CANCEL); 
      mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; 
    } 
    return super.dispatchTouchEvent(ev); 
  } 
 
  // if have a target, see if we're allowed to and want to intercept its 
  // events 
  if (!disallowIntercept && onInterceptTouchEvent(ev)) { 
    final float xc = scrolledXFloat - (float) target.mLeft; 
    final float yc = scrolledYFloat - (float) target.mTop; 
    mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; 
    ev.setAction(MotionEvent.ACTION_CANCEL); 
    ev.setLocation(xc, yc); 
    if (!target.dispatchTouchEvent(ev)) { 
      // target didn't handle ACTION_CANCEL. not much we can do 
      // but they should have. 
    } 
    // clear the target 
    mMotionTarget = null; 
    // Don't dispatch this event to our own view, because we already 
    // saw it when intercepting; we just want to give the following 
    // event to the normal onTouchEvent(). 
    return true; 
  } 
 
  if (isUpOrCancel) { 
    mMotionTarget = null; 
  } 
 
  // finally offset the event to the target's coordinate system and 
  // dispatch the event. 
  final float xc = scrolledXFloat - (float) target.mLeft; 
  final float yc = scrolledYFloat - (float) target.mTop; 
  ev.setLocation(xc, yc); 
 
  if ((target.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { 
    ev.setAction(MotionEvent.ACTION_CANCEL); 
    target.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; 
    mMotionTarget = null; 
  } 
 
  return target.dispatchTouchEvent(ev); 
} 

이 함수 코드 는 비교적 길 어서 우 리 는 위 에 표 시 된 몇 가지 관건 만 볼 수 있다.우선 코드 1 에서 조건 판단 을 볼 수 있 습 니 다.disallow Intercept 과!onIntercept TouchEvent(ev)둘 중 하 나 는 true 이 고 이 조건 판단 에 들 어 갑 니 다.disallow Intercept 은 이벤트 차단 기능 을 사용 하지 않 을 지 여부 입 니 다.기본 값 은 false 이 며,requestDisallow Intercept TouchEvent 방법 을 호출 하여 이 값 을 수정 할 수 있 습 니 다.그러면 첫 번 째 값 이 false 일 때 두 번 째 값 에 전적으로 의존 하여 조건 판단 의 내부 에 들 어 갈 수 있 는 지,두 번 째 값 은 무엇 입 니까?onInterceptTouchEvent 는 ViewGroup 이 이 벤트 를 차단 하 는 함수 입 니 다.이 함 수 를 되 돌려 false 로 돌아 가면 이 벤트 를 차단 하지 않 음 을 표시 하고,반대로 차단 을 표시 합 니 다.두 번 째 조건 은 onInterceptTouchEvent 방법 에 대한 반환 값 을 반대 하 는 것 입 니 다.즉,우리 가 onInterceptTouchEvent 방법 에서 false 를 되 돌려 주면 두 번 째 값 을 true 로 만 들 고 조건 판단 의 내부 에 들 어 갑 니 다.만약 에 우리 가 onInterceptTouchEvent 방법 에서 true 로 돌아 가면 두 번 째 값 의 전 체 를 false 로 바 꿉 니 다.이 조건 판단 에서 벗 어 났 다.예 를 들 어 우 리 는 ListView 슬라이딩 을 실현 하여 특정한 기능 을 삭제 해 야 한다.그러면 onInterceptTouchEvent 에서 true 로 돌아 가 onTouchEvent 에서 관련 판단 논 리 를 실현 하여 이 기능 을 실현 할 수 있다.
코드 1 내부 의 if 에 들 어가 면 for 순환 이 있 습 니 다.현재 View Group 의 모든 하위 child view 를 옮 겨 다 니 고 있 습 니 다.이 이벤트 의 좌 표를 만 지면 child view 의 좌표 범위 내 에서 이 child view 는 이 터치 사건 을 처리 합 니 다.즉,이 child view 의 dispatch Touch Event 를 호출 합 니 다.이 child view 가 ViewGroup 형식 이 라면 위의 판단 을 계속 수행 하고 하위 view 를 옮 겨 다 닙 니 다.이 child view 가 View Group 형식 이 아니라면 이 child view 의 형식 이 겹 쳐 쓰 여 있 지 않 은 한 View 의 dispatchTouchEvent 방법 을 직접 호출 합 니 다.View 의 dispatchTouchEvent 함 수 를 봅 시다.
View 의 Touch 이벤트 배포

/** 
 * Pass the touch screen motion event down to the target view, or this 
 * view if it is the target. 
 * 
 * @param event The motion event to be dispatched. 
 * @return True if the event was handled by the view, false otherwise. 
 */ 
public boolean dispatchTouchEvent(MotionEvent event) { 
  if (!onFilterTouchEventForSecurity(event)) { 
    return false; 
  } 
 
  if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED && 
      mOnTouchListener.onTouch(this, event)) { 
    return true; 
  } 
  return onTouchEvent(event); 
} 

이 함수 에서 먼저 이 사건 이 보안 정책 에 부합 되 는 지 판단 한 다음 에 이 view 가 enable 인지,Touch Listener 가 설정 되 어 있 는 지,mOnTouch Listener 는 우리 가 setOnTouch Listener 를 통 해 설정 한 것 입 니 다.

/** 
 * Register a callback to be invoked when a touch event is sent to this view. 
 * @param l the touch listener to attach to this view 
 */ 
public void setOnTouchListener(OnTouchListener l) { 
  mOnTouchListener = l; 
} 

mOnTouch Listener.onTouch(this,event)가 false 로 돌아 가면 onTouch Event(event)를 계속 실행 합 니 다.mOnTouch Listener.onTouch(this,event)가 true 로 돌아 가면 이 사건 이 소비 되 고 전달 되 지 않 기 때문에 onTouch Event(event)를 실행 하지 않 습 니 다.이것 또한 우리 가 위 에서 남 긴 장면 2 를 검증 했다.onTouch 함수 가 true 로 돌아 갈 때 단 추 를 누 르 지만 우리 의 클릭 사건 은 실행 되 지 않 았 다.그럼 일단 온 터치 이벤트(event)함수 가 무엇 을 했 는 지 살 펴 보 자.

/** 
 * Implement this method to handle touch screen motion events. 
 * 
 * @param event The motion event. 
 * @return True if the event was handled, false otherwise. 
 */ 
public boolean onTouchEvent(MotionEvent event) { 
  final int viewFlags = mViewFlags; 
 
  if ((viewFlags & ENABLED_MASK) == DISABLED)    // 1、   view  enable 
    // A disabled view that is clickable still consumes the touch 
    // events, it just doesn't respond to them. 
    return (((viewFlags & CLICKABLE) == CLICKABLE || 
        (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)); 
  } 
 
  if (mTouchDelegate != null) { 
    if (mTouchDelegate.onTouchEvent(event)) { 
      return true; 
    } 
  } 
 
  if (((viewFlags & CLICKABLE) == CLICKABLE || 
      (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) // 2、   clickable  long clickable 
    switch (event.getAction()) { 
      case MotionEvent.ACTION_UP:          //      
        boolean prepressed = (mPrivateFlags & PREPRESSED) != 0; 
        if ((mPrivateFlags & PRESSED) != 0 || prepressed) { 
          // take focus if we don't have it already and we should in 
          // touch mode. 
          boolean focusTaken = false; 
          if (isFocusable() && isFocusableInTouchMode() && !isFocused()) { 
            focusTaken = requestFocus();    //      
          } 
 
          if (!mHasPerformedLongPress) { 
            // This is a tap, so remove the longpress check 
            removeLongPressCallback(); 
 
            // Only perform take click actions if we were in the pressed state 
            if (!focusTaken) { 
              // Use a Runnable and post this rather than calling 
              // performClick directly. This lets other visual state 
              // of the view update before click actions start. 
              if (mPerformClick == null) { 
                mPerformClick = new PerformClick(); 
              } 
              if (!post(mPerformClick))   // post 
                performClick();     // 3、       
              } 
            } 
          } 
 
          if (mUnsetPressedState == null) { 
            mUnsetPressedState = new UnsetPressedState(); 
          } 
 
          if (prepressed) { 
            mPrivateFlags |= PRESSED; 
            refreshDrawableState(); 
            postDelayed(mUnsetPressedState, 
                ViewConfiguration.getPressedStateDuration()); 
          } else if (!post(mUnsetPressedState)) { 
            // If the post failed, unpress right now 
            mUnsetPressedState.run(); 
          } 
          removeTapCallback(); 
        } 
        break; 
 
      case MotionEvent.ACTION_DOWN: 
        if (mPendingCheckForTap == null) { 
          mPendingCheckForTap = new CheckForTap(); 
        } 
        mPrivateFlags |= PREPRESSED; 
        mHasPerformedLongPress = false; 
        postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); 
        break; 
 
      case MotionEvent.ACTION_CANCEL: 
        mPrivateFlags &= ~PRESSED; 
        refreshDrawableState(); 
        removeTapCallback(); 
        break; 
 
      case MotionEvent.ACTION_MOVE: 
        final int x = (int) event.getX(); 
        final int y = (int) event.getY(); 
 
        // Be lenient about moving outside of buttons 
        int slop = mTouchSlop; 
        if ((x < 0 - slop) || (x >= getWidth() + slop) || 
            (y < 0 - slop) || (y >= getHeight() + slop)) { 
          // Outside button 
          removeTapCallback(); 
          if ((mPrivateFlags & PRESSED) != 0) { 
            // Remove any future long press/tap checks 
            removeLongPressCallback(); 
 
            // Need to switch from pressed to not pressed 
            mPrivateFlags &= ~PRESSED; 
            refreshDrawableState(); 
          } 
        } 
        break; 
    } 
    return true; 
  } 
 
  return false; 
} 

우 리 는 onTouchEvent 함수 에서 바로 ACTION 을 보 았 다.UP、ACTION_DOWN、ACTION_MOVE 등 몇 가지 이 벤트 를 처리 하 는데 가장 중요 한 것 은 UP 이벤트 입 니 다.이 안 에는 사용자 클릭 이벤트 에 대한 처리 나 사용자 에 게 상대 적 으로 중요 한 점 이 포함 되 어 있 기 때문에 첫 번 째 케이스 에 넣 었 습 니 다.ACTION 에서UP 이벤트 에서 이 view 가 enable 인지,clickable 인지,초점 을 얻 었 는 지 판단 한 다음 에 post 방법 을 통 해 PerformClick 대상 을 UI 스 레 드 에 배달 하고 배달 에 실패 하면 permClick 함수 로 클릭 이 벤트 를 직접 호출 합 니 다.

/** 
 * Causes the Runnable to be added to the message queue. 
 * The runnable will be run on the user interface thread. 
 * 
 * @param action The Runnable that will be executed. 
 * 
 * @return Returns true if the Runnable was successfully placed in to the 
 *     message queue. Returns false on failure, usually because the 
 *     looper processing the message queue is exiting. 
 */ 
public boolean post(Runnable action) { 
  Handler handler; 
  if (mAttachInfo != null) { 
    handler = mAttachInfo.mHandler; 
  } else { 
    // Assume that post will succeed later 
    ViewRoot.getRunQueue().post(action); 
    return true; 
  } 
 
  return handler.post(action); 
} 

PerformClick 클래스 를 봅 시다.

private final class PerformClick implements Runnable { 
  public void run() { 
    performClick(); 
  } 
} 

내 부 는 View 클래스 의 performClick()방법 을 포장 한 것 으로 보인다.performClick()방법 보기:

/** 
 * Register a callback to be invoked when this view is clicked. If this view is not 
 * clickable, it becomes clickable. 
 * 
 * @param l The callback that will run 
 * 
 * @see #setClickable(boolean) 
 */ 
 public void setOnClickListener(OnClickListener l) { 
   if (!isClickable()) { 
     setClickable(true); 
   } 
   mOnClickListener = l; 
 } 
 
 /** 
 * Call this view's OnClickListener, if it is defined. 
 * 
 * @return True there was an assigned OnClickListener that was called, false 
 *     otherwise is returned. 
 */ 
 public boolean performClick() { 
   sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); 
 
   if (mOnClickListener != null) { 
     playSoundEffect(SoundEffectConstants.CLICK); 
     mOnClickListener.onClick(this); 
     return true; 
   } 
 
   return false; 
 } 

코드 는 매우 간단 합 니 다.주로 mOnClickListener.onClick(this)을 호출 했 습 니 다.방법 은 사용자 가 setOnClickListener 설정 을 통 해 들 어 온 클릭 이벤트 로 Listener 를 처리 하 는 것 입 니 다.
 
총결산
사용자 터치 스크린 에서 터치 메 시 지 를 만 듭 니 다.시스템 바 텀 에서 이 메 시 지 를 ViewRoot(ViewRootImpl)에 전달 하고 ViewRoot 에서 DISPATECHE 를 만 듭 니 다.POINTER 의 메 시 지 를 handleMessage 에서 처리 하고 최종 적 으로 deliverPointerEvent(MotionEvent event)를 통 해 이 메 시 지 를 처리 합 니 다.이 함수 에 서 는 mView.dispatchTouchEvent(event)를 호출 하여 메 시 지 를 보 냅 니 다.이 mView 는 ViewGroup 형식 이기 때문에 ViewGroup 의 dispatchTouchEvent(event)입 니 다.이 함수 에 서 는 모든 child view 를 옮 겨 다 니 며 이 사건 의 촉발 왼쪽 과 모든 child view 의 좌 표를 비교 합 니 다.만 진 좌표 가 이 child view 범위 내 에 있다 면,이 child view 에서 처리 합 니 다.이 child view 가 ViewGroup 형식 이 라면 이전 검색 과정 을 계속 합 니 다.그렇지 않 으 면 View 의 dispatchTouchEvent(event)함 수 를 실행 합 니 다.View 의 dispatchTouchEvent(event)에서 먼저 이 컨트롤 이 enale 및 mOnTouchListent 가 비어 있 는 지 판단 하고,mOnTouchListener 가 비어 있 지 않 으 면 mOnTouchListener.onTouch(event)방법 을 실행 하 며,이 방법 이 false 로 돌아 가면 View 의 onTouchEvent(event)방법 을 실행 하고,이 방법 에서 mOnClickListener.onClick(this,event)을 실행 합 니 다.방법mOnTouch Listener.onTouch(event)가 true 로 돌아 가면 onTouch Event 방법 을 실행 하지 않 기 때문에 이 벤트 를 클릭 해도 실행 되 지 않 습 니 다.
대략적인 흐름 도 는 다음 과 같다.

본 고 에서 말 한 것 은 모두 가 안 드 로 이 드 프로 그래 밍 디자인 을 더욱 깊이 파악 하 는 데 어느 정도 참고 가치 가 있다 고 믿는다.

좋은 웹페이지 즐겨찾기