手勢(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;
?}});