private float initialX;
private float initialY;
private int mActivePointerId;
private float initialXPress;
private float initialYPress;
private boolean click = true;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
click = true;
//gesture has begun
float x;
float y;
//cancel any current animations
v.clearAnimation();
mActivePointerId = event.getPointerId(0);
x = event.getX();
y = event.getY();
initialXPress = x;
initialYPress = y;
break;
case MotionEvent.ACTION_MOVE:
//gesture is in progress
final int pointerIndex = event.findPointerIndex(mActivePointerId);
//Log.i("pointer index: " , Integer.toString(pointerIndex));
if(pointerIndex < 0 || pointerIndex > 0 ){
break;
}
final float xMove = event.getX(pointerIndex);
final float yMove = event.getY(pointerIndex);
//calculate distance moved
final float dx = xMove - initialXPress;
final float dy = yMove - initialYPress;
//throw away the move in this case as it seems to be wrong
//TODO: figure out why this is the case
if((int)initialXPress == 0 && (int) initialYPress == 0){
//makes sure the pointer is valid
break;
}
//calc rotation here
float posX = v.getX() + dx;
float posY = v.getY() + dy;
//in this circumstance consider the motion a click
if (Math.abs(dx + dy) > 5) click = false;
v.setX(posX);
v.setY(posY);
//card.setRotation
float distObjectX = posX - initialX;
float rotation = ROTATION_DEGREES * 2.f * distObjectX / parentWidth;
v.setRotation(rotation);
float alpha = (((posX - paddingLeft) / (parentWidth * OPACITY_END)));
v.setAlpha(1 - Math.abs(alpha/2));
break;
case MotionEvent.ACTION_UP:
if(event.findPointerIndex(mActivePointerId) == 0) {
callback.cardActionUp();
}
//check if this is a click event and then perform a click
//this is a workaround, android doesn't play well with multiple listeners
if (click) v.performClick();
if (click) return false;
break;
default:
return false;
}
return true;
}
예전에 TouchEvnet 관련해서 작성했던 코드인데 도움이 되실 것 같아서 공유해드립니다.
실제로 구현했던 코드는 터치했던 View와는 다른 View를 컨트롤하는 이벤트라서 잘 작동할지는 모르겠습니다.