package com.example.sampleevent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnTouch;

public class MainActivity extends AppCompatActivity {
@BindView(R.id.view)
View view;
@BindView(R.id.view2)
View view2;
@BindView(R.id.textView)
TextView textView;
GestureDetector detector;

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


view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
float curX = motionEvent.getX();
float curY = motionEvent.getY();

if (action == MotionEvent.ACTION_DOWN) {
println("손가락 눌림 : " + curX + ", " + curY);
} else if (action == MotionEvent.ACTION_MOVE) {
println("손가락 움직임 : " + curX + ", " + curY);
} else if (action == MotionEvent.ACTION_UP) {
println("손가락 뗌 : " + curX + ", " + curY);
}

return true;
}
});
detector=new GestureDetector(this, new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
println("onDonw() 호출됨.");
return true;
}

@Override
public void onShowPress(MotionEvent e) {
println("onShowPress() 호출됨.");
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
println("onSingleTapUp() 호출됨.");
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
println("onScroll 호출됨 : " +distanceX +", "+distanceY );
return false;
}

@Override
public void onLongPress(MotionEvent e) {
println("onLongPress() 호출됨.");
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
println("onFling() 호출됨 : " +velocityX +", "+velocityY);
return true;
}
});
view2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});

}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
Toast.makeText(this,"뒤로가기",Toast.LENGTH_LONG).show();
return true;
}

return false;

}
public void println(String data) {
textView.append(data + "\n");
}
}

activity


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_light"></View>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>

</LinearLayout>


</android.support.constraint.ConstraintLayout>

xml


제스쳐 이벤트중

스크롤:손가락으로 드래그 하는 경우, 이동한 거리 값이 중요

플링: 빠른 속도로 스크롤을 하는 경우, 이동한 속도 값이 중요 





+ Recent posts