Android捕獲點擊事件范圍的方法
View的Tween動畫過程中點擊事件的位置并不會因為動畫位置的改變而改變,是因為在動畫過程中l(wèi)ayout的位置實際上沒有變,因此曾經(jīng)一度認(rèn)為View的點擊事件(其實不僅僅是點擊事件,包括所有的觸摸事件)觸發(fā)的范圍是該View在layout的時候指定的left,top,right,bottom。今天才發(fā)現(xiàn)不完全是這樣的。一切都是因為平時看代碼沒有仔細一點所造成了對問題理解不全面。
在這里記錄一下發(fā)現(xiàn)問題到處理問題的過程。

自定義這樣一個ViewGroup,layout兩個線性布局,左邊的LinearLayout覆蓋全屏幕,右面的LinearLayout在屏幕外面隱藏。然后觀察在想做滑動的過程中,第二個LinearLayout顯示出來的過程中,按鈕Button和第二個線性布局的位置信息:

可以看到,在向左滑第二個線性布顯示出來的過程中,他的位置并沒有變,這里指的是通過getLeft(),getTop(),getRight(),getBottom()獲得的位置,也就是由layout決定的位置。
既然位置并沒有改變,那么這時候點擊第二個線性布局和按鈕點擊事件也被響應(yīng)了,就說明捕獲點擊事件的位置并不完全是在layout的位置。因為并沒有將手伸到屏幕外面去點擊…
回頭來看ViewGroup#dispatchTouchEvent方法在分發(fā)觸摸事件的時候:
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ù)就是判斷點(scrolledXInt,scrolledYInt)是不是在frame矩形里面。這個矩形frame是由child.getHitRect(frame);獲得的:
public void getHitRect(Rect outRect) {
outRect.set(mLeft, mTop, mRight, mBottom);
} 顯然這個矩形就是由該子View的Layout的布局參數(shù)所決定的。但是scrolledXInt和scrolledYInt參數(shù),并不是我們手指點擊的位置:
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;
可以看出,在判斷這個點是否包含在子View內(nèi)的時候,這個點不是手指所點擊的坐標(biāo),而是手指點擊的坐標(biāo)加上了mScrollX和mScrollY,然后在判斷是否在該子View的范圍里面。
現(xiàn)在思考向左滑動的過程中,雖然第二個線性布局的位置沒有變,還是layout的參數(shù)位置,是:mLeft:720,mTop:0,mRight:1440,mBottom:1134。
但是他的父View的mScrollX改變了,向左滑mScrollX大于0,這是用手點擊第二個線性布局,手所點擊的位置再加上mScrollX的值,這時就會落在了第二個線性布局的layout的范圍里面。
測試代碼:
自定義MyViewGroup:
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("點擊按鈕");
ll2.addView(btn);
addView(ll1);
addView(ll2);
setOnTouchListener(new MyTouchEvent());
ll2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "點擊了線性布局2", 0).show();
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "點擊了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里面:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyViewGroup(this));
}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android應(yīng)用內(nèi)調(diào)用第三方應(yīng)用的方法
這篇文章主要介紹了Android應(yīng)用內(nèi)調(diào)用第三方應(yīng)用的方法,有需要的朋友可以參考一下2014-01-01
Android WebView交互傳遞json字符串并解析的方法
這篇文章主要給大家介紹了關(guān)于Android中WebView交互傳遞json字符串并解析的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-05-05
Android輸入框控件ClearEditText實現(xiàn)清除功能
這篇文章主要為大家詳細介紹了Android輸入框控件ClearEditText實現(xiàn)清除功能,感興趣的小伙伴們可以參考一下2016-05-05
Android使用Sensor感應(yīng)器獲取用戶移動方向(指南針原理)
這篇文章主要介紹了Android使用Sensor感應(yīng)器獲取用戶移動方向的方法,實例分析了指南針原理極其應(yīng)用,需要的朋友可以參考下2015-12-12
Android拍照保存在系統(tǒng)相冊不顯示的問題解決方法
我們保存相冊到Android手機的時候,然后去打開系統(tǒng)圖庫找不到我們想要的那張圖片,那是因為我們插入的圖片還沒有更新的緣故,下面與大家分享下此問題的解決方法2013-06-06

