boolean onTouchEvent (MotionEvent event)
boolean onTouch (View v, MotionEvent event)
ACTION_DOWN 화면을 눌렀다.
ACTION_MOVE 누를 채로 움직였다.
ACTION_UP 화면에서 손가락을 뗏다.
2. 키보드 입력
키 입력을 받아 화면상의 원을 상하좌우로 이동 시키는 예제
public class MoveCircle extends Activity {
private MyView vw;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vw = new MyView(this);
vw.setFocusable(true);
vw.setFocusableInTouchMode(true);
setContentView(vw);
}
protected class MyView extends View {
float mX,mY;
int mColor;
public MyView(Context context) {
super(context);
mX = 100;
mY = 100;
mColor = Color.BLUE;
}
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.LTGRAY);
Paint Pnt = new Paint();
Pnt.setColor(mColor);
Pnt.setAntiAlias(true);
canvas.drawCircle(mX,mY,16,Pnt);
}
public boolean onKeyDown(int KeyCode, KeyEvent event) {
super.onKeyDown(KeyCode, event);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (KeyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
mX-=5;
invalidate();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
mX+=5;
invalidate();
return true;
case KeyEvent.KEYCODE_DPAD_UP:
mY-=5;
invalidate();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
mY+=5;
invalidate();
return true;
case KeyEvent.KEYCODE_DPAD_CENTER:
if (mColor == Color.BLUE) {
mColor = Color.RED;
} else {
mColor = Color.BLUE;
}
invalidate();
return true;
}
}
return false;
}
}
}
댓글 없음:
댓글 쓰기