본문 바로가기
programming/android

[Android / 안드로이드] 제스처 이벤트(GestureDetector) 예제

by fluorite94 2018. 12. 1.
반응형

 

 

안드로이드는 사용자의 제스처를 쉽게 구분할 수 있도록 하는 GestureDetector 인터페이스가 있다.

onDown (터치), 

onShowPress (onDown 보다 길게 터치),

onSingleTapUp (터치가 끝날 때),

onLongPress (onShowPress보다 길게 터치),

onScroll(스크롤),

onFling (스크롤과 비슷하지만 손가락으로 튕길 때)

이런 이벤트들을 구분할 수 있다.

 

 

layout

 

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">


    <view android:id="@+id/gestureView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="@color/colorPrimary">
    </view>

    <scrollview android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1">
        <textview android:id="@+id/txtView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textcolor="#000000" android:textsize="16sp" android:text="Gesture sample">
    </textview></scrollview>



</linearlayout>

 

* MainActivity

 

 
... 

public class MainActivity extends AppCompatActivity {
    TextView textView = null;
    GestureDetector gestureDetector = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView)findViewById(R.id.txtView);

        View gestureView = findViewById(R.id.gestureView);
        gestureView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                gestureDetector.onTouchEvent(motionEvent);
                return true;
            }
        });



        gestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent motionEvent) {
                textView.append("onDown Call" + "\n");
                return true;
            }
            @Override
            public void onShowPress(MotionEvent motionEvent) {
                textView.append("onShowPress Call" + "\n");
            }
            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                textView.append("onSingleTapUp Call" + "\n");
                return true;
            }
            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                textView.append("onScroll Call (" + v + " * " +  v1 + ")" + "\n");
                return true;
            }
            @Override
            public void onLongPress(MotionEvent motionEvent) {
                textView.append("onLongPress Call" + "\n");
            }
            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                textView.append("onFling Call (" + v + " * " +  v1 + ")" + "\n");
                return true;
            }
        });
    }
}

 

이렇게 작성하고 실행하면 아래와 같이 확인할 수 있다.

 

 

반응형


댓글