今天就跟大家聊聊有關(guān)怎么在Android中捕獲點(diǎn)擊事件范圍,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
我們提供的服務(wù)有:做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、阿合奇ssl等。為上千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的阿合奇網(wǎng)站制作公司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í)候:
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);獲得的:
public void getHitRect(Rect outRect) { outRect.set(mLeft, mTop, mRight, mBottom); }
顯然這個(gè)矩形就是由該子View的Layout的布局參數(shù)所決定的。但是scrolledXInt和scrolledYInt參數(shù),并不是我們手指點(diǎn)擊的位置:
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的范圍里面。
測試代碼:
自定義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("點(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里面:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyViewGroup(this)); } }Android是什么
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
看完上述內(nèi)容,你們對(duì)怎么在Android中捕獲點(diǎn)擊事件范圍有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
新聞名稱:怎么在Android中捕獲點(diǎn)擊事件范圍-創(chuàng)新互聯(lián)
本文URL:http://bm7419.com/article10/didsgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、服務(wù)器托管、建站公司、Google、企業(yè)建站、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容