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;
}
});
}@Overridepublic void println(String data) {
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
Toast.makeText(this,"뒤로가기",Toast.LENGTH_LONG).show();
return true;
}
return false;
}
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
제스쳐 이벤트중
스크롤:손가락으로 드래그 하는 경우, 이동한 거리 값이 중요
플링: 빠른 속도로 스크롤을 하는 경우, 이동한 속도 값이 중요
'안드로이드 앱프로그래밍 연습' 카테고리의 다른 글
Android AlertDialog 알림 대화상자 보여주기 (0) | 2019.07.12 |
---|---|
Android Toast 토스트,스낵바 그리고 대화상자 사용하기 (0) | 2019.07.12 |
드로어블 만들기 연습 (0) | 2019.07.09 |
SMS 입력 화면 만들고 글자의 수 표시하기 (0) | 2019.07.08 |
두 개의 이미지뷰에 이미지 번갈아 보여주기 (0) | 2019.07.08 |