实例详解用户输入 i. 检测常用手势

2019-12-10 19:14:23于丽

B. 部分手势

如果你仅仅只想处理几种手势,你可以选择继承GestureDetector.SimpleOnGestureListener类,而不是实现
GestureDetector.OnGestureListener接口

MainActivity.java

public class MainActivity extends Activity 
{ 
private GestureDetector mGestureDetector = null; 
private TextView mGestureTextView = null; 
private TextView mDoubleTapTextView = null; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
mGestureTextView = (TextView) findViewById(R.id.gesture); 
mDoubleTapTextView = (TextView) findViewById(R.id.doubleTap); 
// 构造GestureDetector对象,传入监听器对象 
mGestureDetector = new GestureDetector(this, new MyGestureListener()); 
// 传入双击监听器对象 
} 
@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
// 在onTouchEvent方法中将事件传递给手势检测对象,否则手势监听对象中的回调函数是不会被调用的 
this.mGestureDetector.onTouchEvent(event); 
return super.onTouchEvent(event); 
} 
class MyGestureListener extends GestureDetector.SimpleOnGestureListener 
{ 
private static final String DEBUG_TAG = "Gestures"; 
@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
float velocityY) 
{ 
Log.d(DEBUG_TAG, "onFling: " + e1.toString() + ", " + e2.toString()); 
mGestureTextView.setText("onFling "); 
return false; 
} 
@Override 
public boolean onDown(MotionEvent e)
{ 
Log.d(DEBUG_TAG, "onDown: " + e.toString()); 
mGestureTextView.setText("onDown: "); 
return false; 
} 
}} 


注:相关教程知识阅读请移步到Android开发频道。