我有一個自定義的佈局縮放縮放代碼作爲父級和處理單擊功能的子佈局。因此,我使用觸摸攔截,但問題是,它不知道何時點擊或拖動。如何區分拖動和點擊onTouchEvent()?
@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
startClickTime = System.currentTimeMillis(); //start time when first finger land
Log.i("Zoom", " actionDown");
if (scale > MIN_ZOOM){
mode = Mode.DRAG;
startX = ev.getX() - prevDx;
startY = ev.getY() - prevDy;
}
return false; //go to child layout
case MotionEvent.ACTION_POINTER_DOWN:
mode = Mode.ZOOM;
return true;
case MotionEvent.ACTION_UP:
long clickDuration = System.currentTimeMillis() - startClickTime;
mode = Mode.NONE;
if(clickDuration < MAX_CLICK_DURATION){
return false;
}
else {
// letting go from drag or zooming
return true;
}
case MotionEvent.ACTION_MOVE:
clickDuration = System.currentTimeMillis() - startClickTime;
if (clickDuration > MAX_CLICK_DURATION){
return true;
}
else {
return false;
}
}
return false;
}
在我的孩子佈局單擊功能:
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// primary finger down
return true;
case MotionEvent.ACTION_POINTER_DOWN:
// non-primary finger down
return false;
case MotionEvent.ACTION_CANCEL:
return false;
case MotionEvent.ACTION_UP:
// primary finder up
Intent intent = new Intent(context, DeviceActivity.class);
context.startActivity(intent);
return true;
case MotionEvent.ACTION_POINTER_UP:
// non-primary finger up
return false;
}
那麼,有沒有辦法區分拖動和點擊。
你有如何在onInterceptTouchEvent使用它的例子嗎? –
https://developer.android.com/training/gestures/scale.html – Sampath