Implement GestureDetector in Android
You want to implement Gesture on your Android, gladly android has a build in Simple GestureDetector for you. Here is how you use it
Explanation
First we create a GestureDetector instance
private GestureDetector mGestureDetector;
Then we create a GestureListener for us to listen
class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{ ...... }
After creating those 2, we bind our listener to our GestureDetector instance
mGestureDetector = new GestureDetector(this, new LearnGestureListener());
Lastly we need to know where the event listener will be trigger, that is where onTouchEvent comes in
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) .....
}
** The function on our LearnGestureListener class have logical name except onFling which means on swipe.
Reference
Code Shogun
Calandar Android App
Update History
Jan 17, 2012 - Visual Update
private GestureDetector mGestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGestureDetector = new GestureDetector(this, new LearnGestureListener());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event))
return true;
else
return false;
}
class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onSingleTapUp(MotionEvent ev) {
Log.d("onSingleTapUp",ev.toString());
return true;
}
@Override
public void onShowPress(MotionEvent ev) {
Log.d("onShowPress",ev.toString());
}
@Override
public void onLongPress(MotionEvent ev) {
Log.d("onLongPress",ev.toString());
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("onScroll",e1.toString());
return true;
}
@Override
public boolean onDown(MotionEvent ev) {
Log.d("onDownd",ev.toString());
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("d",e1.toString());
Log.d("e2",e2.toString());
return true;
}
}
Explanation
First we create a GestureDetector instance
private GestureDetector mGestureDetector;
Then we create a GestureListener for us to listen
class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{ ...... }
After creating those 2, we bind our listener to our GestureDetector instance
mGestureDetector = new GestureDetector(this, new LearnGestureListener());
Lastly we need to know where the event listener will be trigger, that is where onTouchEvent comes in
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) .....
}
** The function on our LearnGestureListener class have logical name except onFling which means on swipe.
Reference
Code Shogun
Calandar Android App
Update History
Jan 17, 2012 - Visual Update
No comments: