Simple Gesture – Fling | Monkey Can Code


Simple Gesture – Fling
17. November 2010 · 5 comments · Categories: Android, Code · Tags: Android, Fling, Gesture, Touch

In Android, it’s very easy to implement a simple gesture such as Fling (the action of your finger wipe across the screen in a straight line).

for the Event onFling, if you don’t have code to handle finer gesture detection, such as taking into account x,y and velocity, Fling would work whether you swipe up and down / down and up/ left to right / right to left.

In the following example, I will show you how to easily implement a fling gesture to your app (without using Gesture view control), to simply open another screen. (Calling another activity):

1. You need to import the following library to your code:

//gesture
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;

2. Your class needs to implement the OnGestureListner:

public class Tipster extends Activity implements OnClickListener, OnGestureListener{}

3. Create a private variable GestureDetector in your class variable definition:

private GestureDetector myGesture ;

4. On the class’ onCreate, initialize private GestureDetector:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myGesture = new GestureDetector(this);

5. The most important part, is to add onTouchEvent to your class:

@Override
public boolean onTouchEvent(MotionEvent event){
return myGesture.onTouchEvent(event);
}

6. by implementing the class “OnGestureListener”, you will need to implement the following functions:
To open another screen (activity), just write code inside the onFling function. For example, my onFling calls viewHistory function which opens the History screen.

@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
ViewHistory();
return false;
}

@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
return false;
}

To have finer control, such as only do something when swiping from right to left try this:

//these constants are used for onFling
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {

try {
//do not do anything if the swipe does not reach a certain length of distance
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;

// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

}
// left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
ViewHistory();
}
} catch (Exception e) {
// nothing
}
return false;

}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 使用zipalign对齐应用程序
  2. Android面试常客之Handler全解
  3. Android(安卓)应用中十大常见 UX 错误
  4. Android欢迎界面,一个Activity搞定
  5. Android(安卓)学习之路 之 第2组UI组件:Te
  6. 定制Android关机界面
  7. Android(安卓)ProGuard技术详解
  8. Android(安卓)Handler 用法解析
  9. Android生态崛起:上季度Google Play程序商
  10. Android下的BLE编程解析(一)