public class MainActivity extends Activity {
/** Called when the activity is first created. */
private TouchView touchView; //터치 뷰
private TickHandler tickHandler; //정기처리 핸들러
//어플 초기화
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
touchView = new TouchView(this);
setContentView(touchView);
}
//어플 재시작
public void onResume(){
super.onResume();
tickHandler = new TickHandler();
tickHandler.sleep(0);
}
//어플 일시 정지
public void onPause() {
super.onPause(); //정지
tickHandler=null;
}
//sleep
public void onTick(){
touchView.invalidate(); //화면 갱신
if (tickHandler!=null) tickHandler.sleep(100);
}
public class TickHandler extends Handler {
public void handleMessage(Message msg){
onTick();
}
public void sleep(long delayMills){
removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMills);
}
}
}
//TouchView 인벤트를 처리하는 클래스이구요
//터치 이벤트 처리
public class TouchView extends View {
private int touchX = 0; //터치 x좌표
private int touchY= 0; //터치 Y좌표
private int touchAction = -999; //터치 액션
/*private int ballX = 0;
private int ballY = 0;
private int ballAction = -999;*/
//생성자
public TouchView(Context context) {
super(context);
setBackgroundColor(Color.WHITE);
setFocusable(true);
}
//그리기
protected void onDraw(Canvas canvas){
String str;
//그리기 객체 생성
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(16);
//XY좌표그리기
canvas.drawText("TouchXY>"+touchX+","+touchY,0,20*1,paint);
//터치 액션그리기
str="NONE";
if (touchAction==MotionEvent.ACTION_DOWN) str="ACTION_DOWN";
if (touchAction==MotionEvent.ACTION_MOVE) str="ACTION_MOVE";
if (touchAction==MotionEvent.ACTION_UP) str="ACTION_UP";
if (touchAction==MotionEvent.ACTION_CANCEL) str="ACTION_CANCEL";
canvas.drawText("TouchAction>"+str,0,20*2,paint);
/*canvas.drawText("TrackballXY>"+ballX+","+ballY, 0,20*3 , paint);
str="NONE";
if (ballAction==MotionEvent.ACTION_DOWN) str="ACTION_DOWN";
if (ballAction==MotionEvent.ACTION_MOVE) str="ACTION_MOVE";
if (ballAction==MotionEvent.ACTION_UP) str="ACTION_UP";
if (ballAction==MotionEvent.ACTION_CANCEL) str="ACTION_CANCEL";
canvas.drawText("TrackballAction>"+str,0,20*4,paint);*/
}
//터치 이벤트 처리
public boolean onTouchEvent(MotionEvent event){
touchX=(int)event.getX();
touchY=(int)event.getY();
touchAction=event.getAction();
return true;
}
/*public boolean onTrackballEvent(MotionEvent event){
ballX=(int)(event.getX()*100);
ballY=(int)(event.getY()*100);
ballAction=event.getAction();
return true;
}*/
}
터치좌표를 출력하고 Action Down인지 Up인지 Move인지 출력하는 프로그램에
더블터치와 롱터치까지 구현하고싶은데 더블터치 롱터치가 문제입니다 우째해야하죠...??