自定义ViewGroup的探究

2018-11-30  本文已影响0人  wintersweett

先说现象吧,如下是一个比较简单的没有任何功能的一个ViewGroup,写此demo的意图就是了解自定义ViewGroup情况下,内部代码走向

public class HViewGroup extends ViewGroup {

int xFirst;int yFirst;

int mLastX;

int mLastY;

int RawX;

int RawY;

int x;

int y;

boolean intercepted=false;

publicHViewGroup(Context context) {

super(context);

Log.i("zhm","1");

}

publicHViewGroup(Context context, AttributeSet attrs) {

super(context, attrs);Log.i("zhm","2");

}

publicHViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);Log.i("zhm","3");

}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

publicHViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);Log.i("zhm","4");

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

switch (ev.getAction()) {

case MotionEvent.ACTION_DOWN:

Log.i("zhm","onIntercept中down");

xFirst= (int) ev.getX();

yFirst= (int) ev.getY();

Log.i("zhm","down中xFirst=="+xFirst+"==yFirst=="+yFirst);

intercepted=false;

break;

case MotionEvent.ACTION_MOVE:

Log.i("zhm","onIntercept中move");

x= (int) ev.getX();

y= (int) ev.getY();

mLastX=x;

mLastY=y;

Log.i("zhm","move中mLastX=="+mLastX+"==mLastY=="+mLastY);

if (parentMove(ev)) {

intercepted = true;

} else {

intercepted=false;

}

break;

case MotionEvent.ACTION_UP:

Log.i("zhm","onIntercept中up");

x= (int) ev.getX();

y= (int) ev.getY();

RawX=(int)ev.getRawX();

RawY=(int)ev.getRawY();

mLastX=x;

mLastY=y;

Log.i("zhm","up中mLastX=="+mLastX+"==mLastY=="+mLastY);

intercepted=false;

break;

}

//        mLastX=x;

//        mLastY=y;

        return intercepted;

}

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

Log.i("zhm","dispatchTouchEvent");

return super.dispatchTouchEvent(ev);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

Log.i("zhm","onTouch中down");

Log.i("zhm","Touchdown中xFirst=="+event.getX()+"==yFirst=="+event.getY());

break;

case MotionEvent.ACTION_UP:

Log.i("zhm","onTouch中up");

break;

case MotionEvent.ACTION_MOVE:

Log.i("zhm","onTouch中move");

x= (int) event.getX();

y= (int) event.getY();

mLastX=x;

mLastY=y;

Log.i("zhm","touch move中mLastX=="+mLastX+"==mLastY=="+mLastY);

if (parentMove(event)) {

intercepted = true;

} else {

intercepted=false;

}

break;

}

//        if (onInterceptTouchEvent(event)) {

//            Log.i("zhm","onTouchevent true====");

//        } else {

//            Log.i("zhm","onTouchevent false====");

//        }

        return true;

}

//父容器是否move

    private boolean parentMove(MotionEvent event) {

int X= (int) Math.abs(event.getX()-xFirst);

int Y= (int) Math.abs(event.getY()-yFirst);

Log.i("zhm","parentMove中xFirst=="+xFirst+"==yFirst=="+yFirst);

Log.i("zhm","parentMove中X=="+X+"==Y=="+Y);

if (X > Y) {

return true;

} else {

return false;

}

}

}

接下来看Logcat:

11-30 09:57:53.112 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.112 21881-21881/com.example.user.myapplication I/zhm: onIntercept中down

11-30 09:57:53.112 21881-21881/com.example.user.myapplication I/zhm: down中xFirst==144==yFirst==539

11-30 09:57:53.112 21881-21881/com.example.user.myapplication I/zhm: onTouch中down

11-30 09:57:53.112 21881-21881/com.example.user.myapplication I/zhm: Touchdown中xFirst==144.82146==yFirst==539.5908

11-30 09:57:53.418 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.418 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.418 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==155==mLastY==536

11-30 09:57:53.418 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.418 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==11==Y==2

11-30 09:57:53.435 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.435 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.435 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==160==mLastY==535

11-30 09:57:53.435 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.435 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==16==Y==3

11-30 09:57:53.452 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.452 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.452 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==164==mLastY==535

11-30 09:57:53.452 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.452 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==20==Y==3

11-30 09:57:53.468 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.469 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.469 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==167==mLastY==535

11-30 09:57:53.469 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.469 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==23==Y==3

11-30 09:57:53.485 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.485 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.485 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==170==mLastY==534

11-30 09:57:53.485 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.486 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==26==Y==4

11-30 09:57:53.502 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.502 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.502 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==172==mLastY==534

11-30 09:57:53.502 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.502 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==28==Y==4

11-30 09:57:53.519 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.519 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.519 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==174==mLastY==533

11-30 09:57:53.519 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.519 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==30==Y==5

11-30 09:57:53.536 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.536 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.536 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==177==mLastY==533

11-30 09:57:53.536 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.536 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==33==Y==5

11-30 09:57:53.552 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.553 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.553 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==179==mLastY==533

11-30 09:57:53.553 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.553 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==35==Y==5

11-30 09:57:53.569 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.569 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.569 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==181==mLastY==533

11-30 09:57:53.570 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.570 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==37==Y==5

11-30 09:57:53.586 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.586 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.586 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==183==mLastY==532

11-30 09:57:53.586 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.586 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==39==Y==6

11-30 09:57:53.603 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.603 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.603 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==184==mLastY==532

11-30 09:57:53.603 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.603 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==40==Y==6

11-30 09:57:53.700 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.700 21881-21881/com.example.user.myapplication I/zhm: onTouch中move

11-30 09:57:53.700 21881-21881/com.example.user.myapplication I/zhm: touch move中mLastX==184==mLastY==532

11-30 09:57:53.700 21881-21881/com.example.user.myapplication I/zhm: parentMove中xFirst==144==yFirst==539

11-30 09:57:53.700 21881-21881/com.example.user.myapplication I/zhm: parentMove中X==40==Y==6

11-30 09:57:53.700 21881-21881/com.example.user.myapplication I/zhm: dispatchTouchEvent

11-30 09:57:53.700 21881-21881/com.example.user.myapplication I/zhm: onTouch中up

总结规律:代码运行起来,即走了2,所以必须publicHViewGroup(Context context, AttributeSet attrs) {

super(context, attrs);Log.i("zhm","2");

}

当手指触摸或者滑动都会开始触发dispatchTouchEvent方法,只走了一遍onInterceptTouchEvent方法中ACTION_DOWN的情况,--->onTouchEvent中ACTION_DOWN情况----->dispatchTouchEvent方法--->onToucheEvent中ACTION_MOVE------>dispatchTouchEvent方法------>onTouchEvent中ACTION_MOVE......----->dispatchTouchEvent方法------>onTouchEvent中 ACTION_UP

断点跟踪

此处进入ViewGroup,会后续走Activity、View、ViewRootImpl、InputEventReceiver、MotionEvent类
上一篇下一篇

猜你喜欢

热点阅读