activity_main.xml

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

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView textView;

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

textView = findViewById(R.id.textView);

View view = findViewById(R.id.view1);

view.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent mE){
int action = mE.getAction();//어떤 action인지 알기위해
float x = mE.getX();
float y = mE.getY();

if(action == MotionEvent.ACTION_DOWN){
output("touched"+ x +", "+y+"\n");
//append는 계속해서 내용을 추가한다
}else if(action == MotionEvent.ACTION_MOVE){
output("moved"+ x +", "+y+"\n");
}else if(action == MotionEvent.ACTION_UP){
output("gone"+ x +", "+y+"\n");
}
return true;
}
});

//getsture event
View view2 = findViewById(R.id.view2);
view2.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent mE){
gestureDetector.onTouchEvent(mE);
return true;
}
});
gestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener(){
public boolean onDown(MotionEvent mE){
output("onDown() call");
return true;
}
//화면에 터치했다가 떨어질때 발생하는 event
public void onShowPress(MotionEvent mE){
output("onShowPress() call");
}
//한손으로 터치했다 떨어지는 event 처리
public boolean onSingleTapUp(MotionEvent mE){
output("onSingleTapUp() call");
return true;
}
//손가락으로 오랫동안 터치하는 event 처리
public void onLongPress(MotionEvent mE){
output("onLongPress() call");
}
//터치된 채 일정한 속도와 방향으로 움직였다 떨어지는 event
public boolean onScroll(MotionEvent mE, MotionEvent mE2, float distanceX, float distanceY){
output("onScroll() call"+distanceX+", "+distanceY);
return true;
}
//터치된채 가속도를 내서 움직였다 떨어지는 event처리
public boolean onFling(MotionEvent mE, MotionEvent mE2, float vX, float vY){
output("onFling() call"+vX+", "+vY);
return true;
}
});
}
public void output(String data){
textView.append(data+"\n");
}
}

'android studio' 카테고리의 다른 글

방향전환시 상태값 저장  (0) 2019.08.12
xml을 이용한 focus event  (0) 2019.08.12
카드뷰(CardView)  (0) 2019.08.12
버튼 style변경(onDraw(), invalidate())  (0) 2019.08.12
toast message, style  (0) 2019.08.11

+ Recent posts