[Android]通過setOnTouchListener實現移動View功能
需求
有時需要移動View,以前寫的代碼不太好,就上網上找,也不好用,和我自己寫的都有的問題是View 會比光標的位置靠右下方。以前都忍了,偏就偏吧,現在決心要改,也可能是因爲在這個特殊時期比較閒吧。
實現
正如上面所說,靠右下方,而且相比於靠右,靠下的程度更大。這很難不讓人想到是狀態欄造成的影響,而且單單是狀態欄都還不夠,還有一個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
返回的都是相對於整個屏幕的,在測試時,如果不加限制,甚至可以滑動到狀態欄,事件一直有效。
/**
* {@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
函數根本不明白說的什麼意思,甚至它們的實現完全一樣,可能是mNativePtr
或者HISTORY_CURRENT
有什麼不同吧,不管了。到網上查,是相對這個我們設置觸摸事件的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
的左上角相對於父窗體的位置。
當用戶擡手時,判斷是否是移動了,如果移動了,那就不是點擊事件,而且這個移動操作也結束了,恢復原樣,如果不是,那就是點擊事件,就調用performClick
函數。
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();
}
他提醒我不讓我直接調用performClick
,而是使用performClickInternal
,說是爲了保證autofill manager
工作。可是這個函數是個私有函數,可能說的不是我的這種情況吧。
至於perfomClick
的返回值,感覺不重要就不管了。
最後
如果是懸浮窗,view.getTop()
返回的總是0,所以需要通過LayoutParams
來獲取這個top
的值,並且left不再需要。
WindowManager.LayoutParams layoutParams= (WindowManager.LayoutParams) v.getLayoutParams();
top = event.getRawY() -layoutParams.y- y;