手勢處理有兩種中:觸摸和手勢處理遥巴;
觸摸適合簡單的情況:按下伍玖,抬起婿失,移動钞艇,取消,移除邊界豪硅。
@Override
public booleanonTouch(View view,MotionEvent motionEvent) {
//在motionEvent中獲得具體動作參數(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ā)
//觸摸事件傳遞中會說到
return true;
//手指在屏幕上移動
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;
//動作被取消飘弧,被系統(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相對于自身左上角的x坐標(biāo)
getRawX()是表示相對于屏幕左上角的x坐標(biāo)值
使用:獲得控件實(shí)例,設(shè)置觸摸監(jiān)聽
TextView tv = (TextView)findViewById(R.id.text);
textView.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View view) {}});
GestureDetector:這個手勢處理更專業(yè):
GestureDetector類對外提供了兩個接口:OnGestureListener砚著,OnDoubleTapListener進(jìn)行手勢處理:
OnGestureListener有下面的幾個動作:
按下(onDown): 剛剛手指接觸到觸摸屏的那一剎那次伶,就是觸的那一下。
拋擲(onFling): 手指在觸摸屏上迅速移動稽穆,并松開的動作冠王。
長按(onLongPress): 手指按在持續(xù)一段時間,并且沒有松開舌镶。
滾動(onScroll): 手指在觸摸屏上滑動柱彻。
按住(onShowPress): 手指按在觸摸屏上餐胀,它的時間范圍在按下起效哟楷,在長按之前。
抬起(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):雙擊的時候卖擅,第二次按下的時候出發(fā)。
onDoubleTapEvent(MotionEvent e):通知DoubleTap手勢中的事件,包含down磨镶、up和move事件(這里指的是在雙擊之間發(fā)生的事件,例如在同一個地方雙擊會產(chǎn)生DoubleTap手勢健提,而在DoubleTap手勢里面還會發(fā)生down和up事件琳猫,這兩個事件由該函數(shù)通知);雙擊的第二下Touch down和up都會觸發(fā)私痹,可用e.getAction()區(qū)分脐嫂。
onSingleTapConfirmed(MotionEvent e):用來判定該次點(diǎn)擊是SingleTap而不是DoubleTap,如果連續(xù)點(diǎn)擊兩次就是DoubleTap手勢紊遵,如果只點(diǎn)擊一次账千,系統(tǒng)等待一段時間后沒有收到第二次點(diǎn)擊則判定該次點(diǎn)擊為SingleTap而不是DoubleTap,然后觸發(fā)SingleTapConfirmed事件暗膜。這個方法不同于onSingleTapUp匀奏,他是在GestureDetector確信用戶在第一次觸摸屏幕后,沒有緊跟著第二次觸摸屏幕学搜,也就是不是“雙擊”的時候觸發(fā)
SimpleOnGestureListener
當(dāng)手勢過多娃善,我們只想用其中的一個或幾個,不需要全部從寫瑞佩,那么繼承來自SimpleOnGestureListener是最好的聚磺,內(nèi)部已經(jīng)幫我們實(shí)現(xiàn)了以上的接口;
如何使用:
1.創(chuàng)建實(shí)例:
GestureDetectorCompat detector炬丸;
detector =newGestureDetectorCompat(Context context,OnGestureListener listener);
參數(shù)說明:
context ?上下文對象瘫寝;
listener:OnGestureListener的實(shí)現(xiàn)類
2.將手勢檢測器設(shè)置為雙次敲擊偵聽器。
detector.setOnDoubleTapListener(OnDoubleTapListener listener);
listener:為OnDoubleTapListener接口的實(shí)現(xiàn)類
3.為控件添加手勢處理事件稠炬;
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;
?}});