2013-05-23 3 views
0

안드로이드 뷰에서 대각선 스 와이프 이벤트를 계산하는 데 문제가 있습니다. 어느 누구도 Diagonal 제스처를 식별 할 수있는 논리를 제공 할 수 있습니까? 내 요구 사항 : 사용자가 전화에서 대각선으로 스 와이프하면 다른 이벤트를 트리거해야합니다. 좋아요 : 1) TopLeftToBottomRight. 2) TopRightToBottomLeft. 3) 하단 왼쪽 상단. 4) BottomRightToTopLeft.안드로이드에서 대각선 동작을 처리하는 방법?

제발, 어느 누구도이 모든 것을 처리 할 수있는 논리가 있습니까? 샘플 신청서를 보내 주시면 감사하겠습니다. 이 일을 최대한 빨리 끝내야합니다. 미리 감사드립니다.

답변

1

터치 이벤트 및 모션 이벤트를 인식하려면 GestureDectector을 사용해야합니다. 화면의 초기 및 최종 좌표가 표시됩니다. 그런 다음 알고리즘을 호출하여 얻을 수있는 좌표에 따라 스 와이프 제스처의 방향을 결정해야합니다. 그렇게 어렵지 않습니다. GestureDetector 사용 방법은 a link입니다.

당신이 당신의 방법을 작동하도록하는 방법 onFling이 코드를 개선 할 수 있습니다

여기에 왼쪽과 오른쪽 강타와 터치 이벤트를 인식하기위한 샘플입니다.

package com.ex.gessture; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.FrameLayout; 

public class GestureListenerActivity extends Activity { 

FrameLayout frame; 
GestureDetector mGestureDetector; 
String TAG = "GestureListenerActivity"; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    frame = (FrameLayout) findViewById(R.id.frame); 

    mGestureDetector = new GestureDetector(this, mGestureListener); 

    frame.setOnTouchListener(new FrameLayout.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      return mGestureDetector.onTouchEvent(event); 
     } 
    }); 
} 

/** 
* Gesture Event Handler 
*/ 
private SimpleOnGestureListener mGestureListener = new SimpleOnGestureListener() { 

    int swipe_Min_Distance = 100; 
    int swipe_Max_Distance = 350; 
    int swipe_Min_Velocity = 100; 
/*  
    @Override 
    public boolean onDown(MotionEvent e) { 
     Log.i(TAG, "[CALLBACK_GL] boolean onDown(e:" + e + ")"); 
     return super.onDown(e); 
    } 
*/ 
    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) 
    { 

     /** 
     * 
     * Do your stuff 
     * 
     */ 

     return super.onSingleTapUp(e); 
    } 

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
     Log.i(TAG, "[CALLBACK_GL] boolean onSingleTapConfirmed(e:" + e + ")"); 
     return super.onSingleTapConfirmed(e); 
    } 

    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
     Log.i(TAG, "[CALLBACK_GL] boolean onDoubleTap(e:" + e + ")"); 


     return super.onDoubleTap(e); 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     Log.i(TAG, "[CALLBACK_GL] boolean onFling(e1:" + e1 + ", e2:" + e2 + ", velocityX:" + velocityX 
       + ", velocityY:" + velocityY + ""); 

     final float xDistance = Math.abs(e1.getX() - e2.getX()); 
     final float yDistance = Math.abs(e1.getY() - e2.getY()); 

     if(xDistance > this.swipe_Max_Distance || yDistance > this.swipe_Max_Distance) 
      return false; 

     velocityX = Math.abs(velocityX); 
     velocityY = Math.abs(velocityY); 
     boolean result = false; 

     if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance){ 
      if(e1.getX() > e2.getX()) // right to left 
       Log.i(TAG, "Swipe Left"); 

      else 
       Log.i(TAG, "Swipe Right"); 
     } 

     //return super.onFling(e1, e2, velocityX, velocityY); 
     return true; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) { 
     Log.i(TAG, "[CALLBACK_GL] void onShowPress(e:" + e + ")"); 
     super.onShowPress(e); 
    } 

    @Override 
    public void onLongPress(MotionEvent e) { 
     Log.i(TAG, "[CALLBACK_GL] void onLongPress(e:" + e + ")"); 

     super.onLongPress(e); 
    } 
}; 

}  

예 : 사용 TopRightToBottomLeft 슬쩍에 대한 onFling에서이 조건 :

// TopRightToBottomLeft 
if((e1.getX() > e2.getX()) && (e1.getY() > e2.getY())) { 
    Log.i(TAG, "Swipe TopRightToBottomLeft"); 
} 

당신은 다른 이벤트를 유사하게 시도 할 수 있습니다.

+0

vickey 내 문제를 조사 주셔서 너무 감사합니다 사용자 지정 제스처를 만들 수 GestureOverlayView 및 시스템 응용 프로그램이라고 GestureBuilder을 사용하는 것입니다. onFling()은 rightToLeft, LeftToRight, TopToBottom 및 BottomToTop에 대해서만 제공합니다. 이건 내가 제대로 했어. 대각선 사건을 계산하는데 문제가있다. (대각선 스 와이프 이벤트를 onFling() 함수와 구별하기 위해 수학적 계산이 필요합니다.) 다시 한 번 재생에 감사드립니다. – Vishnuvardhan

+0

편집 내용을 확인하십시오. –

0

GestureDetector를 사용할 때 직접 각각의 제스처를 인식해야 할 것이다는 더 나은 솔루션

+0

감사합니다 pskink, 지금 나는 GestureDetector만을 사용하고 있으며, 그들을 사용하여 touchDown 및 touchUp을 계산하는 것은 이벤트 유형을 계산하고 있습니다. – Vishnuvardhan

관련 문제