public
class
GestureActivity
extends
Activity {
private
GestureDetector gestureDetector;
private
Screen screen;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector =
new
GestureDetector(
this
,onGestureListener);
screen = GestureUtils.getScreenPix(
this
);
}
@Override
public
boolean
onTouchEvent(MotionEvent event) {
return
gestureDetector.onTouchEvent(event);
}
GestureDetector.OnGestureListener OnGestureListener=
new
GestureDetector.SimpleOnGestureListener(){
@Override
public
boolean
onFling(MotionEvent e1, MotionEvent e2,
float
velocityX,
float
velocityY) {
float
x = e2.getX() - e1.getX();
float
y = e2.getY() - e1.getY();
float
x_limit = screen.widthPixels /
3
;
float
y_limit = screen.heightPixels /
3
;
float
x_abs = Math.abs(x);
float
y_abs = Math.abs(y);
if
(x_abs >= y_abs){
if
(x > x_limit || x <-x_limit){
if
(x>
0
){
show(
"right"
);
}
else
if
(x
show(
"left"
);
}
}
}
else
{
if
(y > y_limit || y <-y_limit){
if
(y>
0
){
show(
"down"
);
}
else
if
(y
show(
"up"
);
}
}
}
return
true
;
}
};
private
void
show(String value){
Toast.makeText(
this
, value, Toast.LENGTH_SHORT).show();
}
}