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

效果:一个手指实现(所有手势事件)和(部分事件的);

A. 所有手势

activity_main.xml

android:id="@+id/gesture"

android:layout_width="match_parent"

android:layout_height="150dp"

android:gravity="center"

android:text="单击手势变化" />

android:id="@+id/doubleTap"

android:layout_width="match_parent"

android:layout_height="150dp"

android:gravity="center"

android:text="双击手势变化" />

MainActivity.java

public class MainActivity extends Activity

{

private static final String LOG_TAG = "HelloGesture";

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, mOnGestureListener);

// 传入双击监听器对象

mGestureDetector.setOnDoubleTapListener(mDoubleTapListener);

}

@Override public boolean onTouchEvent(MotionEvent event)

{

// 在onTouchEvent方法中将事件传递给手势检测对象,否则手势监听对象中的回调函数是不会被调用的

mGestureDetector.onTouchEvent(event);

return super.onTouchEvent(event);

}

private OnGestureListener mOnGestureListener = new OnGestureListener()

{

@Override

public boolean onSingleTapUp(MotionEvent e)

{

Log.i(LOG_TAG, "onSingleTapUp: " + e.toString());

mGestureTextView.setText("onSingleTapUp: ");

return false;

}

@Override

public void onShowPress(MotionEvent e)

{

Log.i(LOG_TAG, "onShowPress: " + e.toString());

mGestureTextView.setText("onShowPress: ");

}

@Override

public boolean onScroll(MotionEvent e1, MotionEvent e2,

float distanceX, float distanceY)

{

Log.i(LOG_TAG, "onScroll: " + e1.toString() + ", " + e2.toString());

mGestureTextView.setText("onScroll ");

return false;

} @Override

public void onLongPress(MotionEvent e)

{

Log.i(LOG_TAG, "onLongPress: " + e.toString());

mGestureTextView.setText("onLongPress: ");

}

@Override

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY)

{

Log.i(LOG_TAG, "onFling: " + e1.toString() + ", " + e2.toString());

mGestureTextView.setText("onFling ");

return false;

}

@Override

public boolean onDown(MotionEvent e)

{

Log.i(LOG_TAG, "onDown: " + e.toString());

mGestureTextView.setText("onDown: ");

return false;

}

}; private OnDoubleTapListener mDoubleTapListener = new OnDoubleTapListener()

{

@Override

public boolean onSingleTapConfirmed(MotionEvent e)

{

Log.i("LOG_TAG", "onSingleTapConfirmed: " + e.toString());

mDoubleTapTextView.setText("onSingleTapConfirmed: ");

return false;

}

@Override

public boolean onDoubleTapEvent(MotionEvent e)

{

Log.i("LOG_TAG", "onDoubleTapEvent: " + e.toString());

mDoubleTapTextView.setText("onDoubleTapEvent: ");

return false;

}

@Override

public boolean onDoubleTap(MotionEvent e)

{

Log.i("LOG_TAG", "onDoubleTap: " + e.toString());

mDoubleTapTextView.setText("onDoubleTap: ");

return false;

}

};}

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;

}

}}

相关阅读:

javascript每日必学之运算符

MySQL中导出用户权限设置的脚本分享

javaScript事件机制兼容【详细整理】

可选择和输入的下拉列表框示例

php过滤所有恶意字符(批量过滤post,get敏感数据)

MySQL 5.5主从同步设置笔记分享

jQuery插件Validate实现自定义表单验证

在JavaScript中处理时间之setMinutes()方法的使用

Cygwin中安装PHP方法步骤

注意:php5.4删除了session_unregister函数

C++三色球问题描述与算法分析

C#使用foreach语句遍历堆栈(Stack)的方法

JavaScript异步编程Promise模式的6个特性

javascript随机将第一个dom中的图片添加到第二个div中示例

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐