마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

ontouch리스너에서 action_move만을 가지고 방향 알수 있는방법있나요

0 추천
제목 그대로입니다.

 

밑에 질문했는데.. action_down이 말을듣지 않네요.

 

그래서 action_move만을 갖고 스크롤 방향(위, 아래)을 아는 코드를 작성하고싶어요

 

도와주십쇼
익명사용자 님이 2016년 10월 24일 질문

1개의 답변

0 추천
    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를 컨트롤하는 이벤트라서 잘 작동할지는 모르겠습니다.

minor (13,710 포인트) 님이 2016년 10월 25일 답변
...