[Android]通過setOnTouchListener實現(xiàn)移動View功能
需求
有時需要移動View,以前寫的代碼不太好,就上網(wǎng)上找,也不好用,和我自己寫的都有的問題是View 會比光標的位置靠右下方。以前都忍了,偏就偏吧,現(xiàn)在決心要改,也可能是因為在這個特殊時期比較閑吧。
實現(xiàn)
正如上面所說,靠右下方,而且相比于靠右,靠下的程度更大。這很難不讓人想到是狀態(tài)欄造成的影響,而且單單是狀態(tài)欄都還不夠,還有一個ToolBar
,所以要點在于如何去除這個高度。
/**
* Returns the original raw Y coordinate of this event. For touch
* events on the screen, this is the original location of the event
* on the screen, before it had been adjusted for the containing window
* and views.
*
* @see #getY(int)
* @see #AXIS_Y
*/
public final float getRawY() {
return nativeGetRawAxisValue(mNativePtr, AXIS_Y, 0, HISTORY_CURRENT);
}
getRawX
或者getRawY
返回的都是相對于整個屏幕的,在測試時,如果不加限制,甚至可以滑動到狀態(tài)欄,事件一直有效。
/**
* {@link #getY(int)} for the first pointer index (may be an
* arbitrary pointer identifier).
*
* @see #AXIS_Y
*/
public final float getY() {
return nativeGetAxisValue(mNativePtr, AXIS_Y, 0, HISTORY_CURRENT);
}
可是看這個getX
函數(shù)根本不明白說的什么意思,甚至它們的實現(xiàn)完全一樣,可能是mNativePtr
或者HISTORY_CURRENT
有什么不同吧,不管了。到網(wǎng)上查,是我們觸摸的點相對這個我們設置觸摸事件的View
的位置(下面有圖)。
在每次設定位置時,需要的是設置當前的View
相對于父View
的坐標,在每次移動時getRawY
的值需要減去getY
的值,得到的值就是當前view相對整個屏幕的位置,在減去當前view
相對于父窗體的位置,這樣得到的值就是父窗體相對于整個屏幕的位置,最后得到的值是跟我們沒有關系的,在獲得當前view
需要的位置時在減去這個值。父窗體是不會動的,所以后者的這個位置可以在MotionEvent.ACTION_DOWN
時獲取,同樣的,getY
的值也需要在這個時候獲取,因為光標相對于當前的View
的位置也是不能改變的。
像這樣:
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
left = event.getRawX() - view.getLeft() - x;
top = event.getRawY() - view.getTop() - y;
return true;
當用戶只是點擊時,是不會有MotionEvent.ACTION_MOVE
事件的,所以在這個事件下記錄當前用戶是否進行的是移動操作。
case MotionEvent.ACTION_MOVE:
if (!moved) {
moved = true;
}
setXPosition(event.getRawX() - x - left);
setYPosition(event.getRawY() - y - top);
return true;
我們用光標相對于整個屏幕的位置減去父窗體的位置,減去光標相對于父窗體的位置,就是當前view
的左上角相對于父窗體的位置。
當用戶抬手時,判斷是否是移動了,如果移動了,那就不是點擊事件,而且這個移動操作也結束了,恢復原樣,如果不是,那就是點擊事件,就調(diào)用performClick
函數(shù)。
case MotionEvent.ACTION_UP:
if (moved) {
moved = false;
} else {
v.performClick();
}
return true;
至于那個performClick
:
/**
* Call this view's OnClickListener, if it is defined. Performs all normal
* actions associated with clicking: reporting accessibility event, playing
* a sound, etc.
*
* @return True there was an assigned OnClickListener that was called, false
* otherwise is returned.
*/
這樣onClickListener
就能夠正常運行了,而不是被我們阻止了。
這還沒有完:
/**
* Entry point for {@link #performClick()} - other methods on View should call it instead of
* {@code performClick()} directly to make sure the autofill manager is notified when
* necessary (as subclasses could extend {@code performClick()} without calling the parent's
* method).
*/
private boolean performClickInternal() {
// Must notify autofill manager before performing the click actions to avoid scenarios where
// the app has a click listener that changes the state of views the autofill service might
// be interested on.
notifyAutofillManagerOnClick();
return performClick();
}
他提醒我不讓我直接調(diào)用performClick
,而是使用performClickInternal
,說是為了保證autofill manager
工作。可是這個函數(shù)是個私有函數(shù),可能說的不是我的這種情況吧。
至于perfomClick
的返回值,感覺不重要就不管了。如果想用這個返回值,就把v.performClick();
部分改為return v.performClick();
。
最后
如果是懸浮窗,view.getTop()
返回的總是0,所以需要通過LayoutParams
來獲取這個top
的值,并且left不再需要,因為它一直等于零。
WindowManager.LayoutParams layoutParams= (WindowManager.LayoutParams) v.getLayoutParams();
top = event.getRawY() -layoutParams.y- y;