Android 手勢(shì)處理

手勢(shì)處理有兩種中:觸摸和手勢(shì)處理;

觸摸適合簡(jiǎn)單的情況:按下,抬起,移動(dòng),取消,移除邊界。

@Override

public booleanonTouch(View view,MotionEvent motionEvent) {

//在motionEvent中獲得具體動(dòng)作參數(shù)

intaction = MotionEventCompat.getActionMasked(motionEvent);

switch(action) {

//按下

case(MotionEvent.ACTION_DOWN) :

Log.i("tedu","Action was DOWN"+"x="+ motionEvent.getX()+"\ny="+ motionEvent.getY());

//返回true 是完全消費(fèi),onclick方法不能處罰,false不完全消費(fèi),onclick方法依然能觸發(fā)

//觸摸事件傳遞中會(huì)說到

return true;

//手指在屏幕上移動(dòng)

case(MotionEvent.ACTION_MOVE) :

Log.i("tedu","Action was MOVE"+"x="+ motionEvent.getX()+"\ny="+ motionEvent.getY());

return true;

//手指離開屏幕

case(MotionEvent.ACTION_UP) :

Log.i("tedu","Action was UP"+"x="+ motionEvent.getX()+"\ny="+ motionEvent.getY());

return true;

//動(dòng)作被取消,被系統(tǒng)取消

case(MotionEvent.ACTION_CANCEL) :

Log.i("tedu","Action was CANCEL");

return true;

//離開邊界

case(MotionEvent.ACTION_OUTSIDE) :

Log.i("tedu","Movement occurred outside bounds "+

"of current screen element");

return true;

default:

return false;

}}

提示:通過motionEvent獲得手指坐標(biāo)的方法getX(),getY(),getRawX(),getRawY()

getX()是表示W(wǎng)idget相對(duì)于自身左上角的x坐標(biāo)

getRawX()是表示相對(duì)于屏幕左上角的x坐標(biāo)值

使用:獲得控件實(shí)例,設(shè)置觸摸監(jiān)聽

TextView tv = (TextView)findViewById(R.id.text);

textView.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View view) {}});

GestureDetector:這個(gè)手勢(shì)處理更專業(yè):

GestureDetector類對(duì)外提供了兩個(gè)接口:OnGestureListener,OnDoubleTapListener進(jìn)行手勢(shì)處理:

OnGestureListener有下面的幾個(gè)動(dòng)作:

按下(onDown): 剛剛手指接觸到觸摸屏的那一剎那,就是觸的那一下。

拋擲(onFling): 手指在觸摸屏上迅速移動(dòng),并松開的動(dòng)作。

長(zhǎng)按(onLongPress): 手指按在持續(xù)一段時(shí)間,并且沒有松開。

滾動(dòng)(onScroll): 手指在觸摸屏上滑動(dòng)。

按住(onShowPress): 手指按在觸摸屏上,它的時(shí)間范圍在按下起效,在長(zhǎng)按之前。

抬起(onSingleTapUp):手指離開觸摸屏的那一剎那。

onScroll(MotionEvent motionEvent,MotionEvent motionEvent1, float v , float v1)

motionEvent:

The first down motion event that started the ? scrolling.

motionEvent1:

The move motion event that triggered the current onScroll.

v:

The distance along the X axis that has been scrolled ? since the last call to onScroll. This is NOT the distance between?e1?and?e2.

v1:

The distance along the Y axis that has been scrolled ? since the last call to onScroll. This is NOT the distance between?e1?and?e2.

參數(shù)說明:

OnDoubleTapListener

onDoubleTap(MotionEvent e):雙擊的時(shí)候,第二次按下的時(shí)候出發(fā)。

onDoubleTapEvent(MotionEvent e):通知DoubleTap手勢(shì)中的事件,包含down、up和move事件(這里指的是在雙擊之間發(fā)生的事件,例如在同一個(gè)地方雙擊會(huì)產(chǎn)生DoubleTap手勢(shì),而在DoubleTap手勢(shì)里面還會(huì)發(fā)生down和up事件,這兩個(gè)事件由該函數(shù)通知);雙擊的第二下Touch down和up都會(huì)觸發(fā),可用e.getAction()區(qū)分。

onSingleTapConfirmed(MotionEvent e):用來判定該次點(diǎn)擊是SingleTap而不是DoubleTap,如果連續(xù)點(diǎn)擊兩次就是DoubleTap手勢(shì),如果只點(diǎn)擊一次,系統(tǒng)等待一段時(shí)間后沒有收到第二次點(diǎn)擊則判定該次點(diǎn)擊為SingleTap而不是DoubleTap,然后觸發(fā)SingleTapConfirmed事件。這個(gè)方法不同于onSingleTapUp,他是在GestureDetector確信用戶在第一次觸摸屏幕后,沒有緊跟著第二次觸摸屏幕,也就是不是“雙擊”的時(shí)候觸發(fā)

SimpleOnGestureListener

當(dāng)手勢(shì)過多,我們只想用其中的一個(gè)或幾個(gè),不需要全部從寫,那么繼承來自SimpleOnGestureListener是最好的,內(nèi)部已經(jīng)幫我們實(shí)現(xiàn)了以上的接口;

如何使用:

1.創(chuàng)建實(shí)例:

GestureDetectorCompat detector;

detector =newGestureDetectorCompat(Context context,OnGestureListener listener);

參數(shù)說明:

context ?上下文對(duì)象;

listener:OnGestureListener的實(shí)現(xiàn)類

2.將手勢(shì)檢測(cè)器設(shè)置為雙次敲擊偵聽器。

detector.setOnDoubleTapListener(OnDoubleTapListener listener);

listener:為OnDoubleTapListener接口的實(shí)現(xiàn)類

3.為控件添加手勢(shì)處理事件;

TextView tv = (TextView)findViewById(R.id.text);

tv.setOnTouchListener(newOnTouchListener() {

public boolean onTouch(View?v,?MotionEvent?event)?{

MainActivity.this.detector.onTouchEvent(event);

if (mGestureDetector.onTouchEvent(event))

return true;

else ?return false;

?}});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 概述 當(dāng)Android系統(tǒng)捕獲到觸摸事件后,如何準(zhǔn)確地傳遞給真正需要這個(gè)事件的View呢?Android系統(tǒng)給...
    小蕓論閱讀 5,270評(píng)論 2 38
  • 一、 Android分發(fā)機(jī)制概述: Android如此受歡迎,就在于其優(yōu)秀的交互性,這其中,Android優(yōu)秀...
    IT楓閱讀 2,647評(píng)論 2 9
  • ViewDragHelper實(shí)例的創(chuàng)建 ViewDragHelper重載了兩個(gè)create()靜態(tài)方法public...
    傀儡世界閱讀 726評(píng)論 0 3
  • 實(shí)戰(zhàn)營(yíng)已經(jīng)開營(yíng)15天了。這些天來,你覺得自己堅(jiān)持的效果如何?產(chǎn)生了什么變化嗎?對(duì)接下來15天,你有什么期待?來,和...
    LindaGE閱讀 499評(píng)論 0 1
  • day113 7.15 啊澤 今天很認(rèn)真的寫這篇感悟。 有時(shí)候覺得和一位優(yōu)秀的前輩交流,真的是這一生中能夠快速學(xué)習(xí)...
    方方不方呀閱讀 164評(píng)論 0 2

友情鏈接更多精彩內(nèi)容