View的Tween動(dòng)畫過程中點(diǎn)擊事件的位置并不會(huì)因?yàn)閯?dòng)畫位置的改變而改變,是因?yàn)樵趧?dòng)畫過程中l(wèi)ayout的位置實(shí)際上沒有變,因此曾經(jīng)一度認(rèn)為View的點(diǎn)擊事件(其實(shí)不僅僅是點(diǎn)擊事件,包括所有的觸摸事件)觸發(fā)的范圍是該View在layout的時(shí)候指定的left,top,right,bottom。今天才發(fā)現(xiàn)不完全是這樣的。一切都是因?yàn)槠綍r(shí)看代碼沒有仔細(xì)一點(diǎn)所造成了對(duì)問題理解不全面。
在這里記錄一下發(fā)現(xiàn)問題到處理問題的過程。
自定義這樣一個(gè)ViewGroup,layout兩個(gè)線性布局,左邊的LinearLayout覆蓋全屏幕,右面的LinearLayout在屏幕外面隱藏。然后觀察在想做滑動(dòng)的過程中,第二個(gè)LinearLayout顯示出來的過程中,按鈕Button和第二個(gè)線性布局的位置信息:
可以看到,在向左滑第二個(gè)線性布顯示出來的過程中,他的位置并沒有變,這里指的是通過getLeft(),getTop(),getRight(),getBottom()獲得的位置,也就是由layout決定的位置。
既然位置并沒有改變,那么這時(shí)候點(diǎn)擊第二個(gè)線性布局和按鈕點(diǎn)擊事件也被響應(yīng)了,就說明捕獲點(diǎn)擊事件的位置并不完全是在layout的位置。因?yàn)椴]有將手伸到屏幕外面去點(diǎn)擊…
回頭來看ViewGroup#dispatchTouchEvent方法在分發(fā)觸摸事件的時(shí)候:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
for ( int i = count - 1 ; i >= 0 ; i--) { final View child = children[i]; if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null ) { child.getHitRect(frame); if (frame.contains(scrolledXInt, scrolledYInt)) { // 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)) { // Event handled, we have a target now. mMotionTarget = child; return true ; } } } |
其中frame.contains(scrolledXInt, scrolledYInt)函數(shù)就是判斷點(diǎn)(scrolledXInt,scrolledYInt)是不是在frame矩形里面。這個(gè)矩形frame是由child.getHitRect(frame);獲得的:
1
2
3
|
public void getHitRect(Rect outRect) { outRect.set(mLeft, mTop, mRight, mBottom); } |
顯然這個(gè)矩形就是由該子View的Layout的布局參數(shù)所決定的。但是scrolledXInt和scrolledYInt參數(shù),并不是我們手指點(diǎn)擊的位置:
1
2
3
4
5
6
7
8
|
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 int scrolledXInt = ( int ) scrolledXFloat; final int scrolledYInt = ( int ) scrolledYFloat; |
可以看出,在判斷這個(gè)點(diǎn)是否包含在子View內(nèi)的時(shí)候,這個(gè)點(diǎn)不是手指所點(diǎn)擊的坐標(biāo),而是手指點(diǎn)擊的坐標(biāo)加上了mScrollX和mScrollY,然后在判斷是否在該子View的范圍里面。
現(xiàn)在思考向左滑動(dòng)的過程中,雖然第二個(gè)線性布局的位置沒有變,還是layout的參數(shù)位置,是:mLeft:720,mTop:0,mRight:1440,mBottom:1134。
但是他的父View的mScrollX改變了,向左滑mScrollX大于0,這是用手點(diǎn)擊第二個(gè)線性布局,手所點(diǎn)擊的位置再加上mScrollX的值,這時(shí)就會(huì)落在了第二個(gè)線性布局的layout的范圍里面。
測(cè)試代碼:
自定義MyViewGroup:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
public class MyViewGroup extends ViewGroup { public static final String TAG = "MyViewGroup" ; private int childCount; private GestureDetector detector; private Button btn; private LinearLayout ll2; public MyViewGroup(Context context, AttributeSet attrs, int defStyle) { super (context, attrs, defStyle); init(context); } public MyViewGroup(Context context, AttributeSet attrs) { super (context, attrs); init(context); } public MyViewGroup(Context context) { super (context); init(context); } private void init( final Context context) { detector = new GestureDetector(context, new MyOnGestureListener()); LinearLayout ll1 = new LinearLayout(context); ll1.setBackgroundColor(Color.BLUE); ll2 = new LinearLayout(context); ll2.setBackgroundColor(Color.RED); btn = new Button(context); btn.setText( "點(diǎn)擊按鈕" ); ll2.addView(btn); addView(ll1); addView(ll2); setOnTouchListener( new MyTouchEvent()); ll2.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "點(diǎn)擊了線性布局2" , 0 ).show(); } }); btn.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "點(diǎn)擊了Button" , 0 ).show(); } }); } @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { super .onMeasure(widthMeasureSpec, heightMeasureSpec); childCount = getChildCount(); for ( int i = 0 ; i < childCount; i++) { View child = getChildAt(i); child.measure(widthMeasureSpec,heightMeasureSpec); } } @Override protected void onLayout( boolean changed, int l, int t, int r, int b) { for ( int i = 0 ; i < childCount; i++) { View child = getChildAt(i); child.layout( 0 +i*getWidth(), 0 , (i+ 1 )*getWidth(), getHeight()); } } private class MyTouchEvent implements View.OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { detector.onTouchEvent(event); return true ; } } private class MyOnGestureListener extends SimpleOnGestureListener{ @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { scrollBy(( int ) distanceX, 0 ); if (getScrollX()% 10 == 0 ) { Log.i(TAG, "Button左上右下位置:" + btn.getLeft() + "/" + btn.getTop() + "/" + btn.getRight() + "/" + btn.getBottom()); Log.i(TAG, "線性布局2的左上右下位置:" + ll2.getLeft() + "/" + ll2.getTop() + "/" + ll2.getRight() + "/" + ll2.getBottom()); Log.i(TAG, "MyViewGroup的mScrollX:" + getScrollX()); } return super .onScroll(e1, e2, distanceX, distanceY); } } } |
然后在Activity里面:
1
2
3
4
5
6
7
8
|
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView( new MyViewGroup( this )); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/cauchyweierstrass/article/details/41862523