首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

利用OnGestureListener辨别用户手势

2012-08-24 
利用OnGestureListener识别用户手势文件引用:http://samwong.iteye.com/blog/870779?android可以识别用户

利用OnGestureListener识别用户手势

文件引用:http://samwong.iteye.com/blog/870779

?

android可以识别用户的手势(即用户用手指滑动的方向),通过用户不同的手势,从而做出不同的处理,下面是一个识别用户手势的效果:

利用OnGestureListener辨别用户手势

?

实现过程是,继承OnGestureListener方法,通过坐标来实现。下面是主要代码:

public class GestureTest extends Activity implements OnTouchListener,
??????? OnGestureListener {

??? GestureDetector mGestureDetector;
??? private static final int FLING_MIN_DISTANCE = 100;
??? private static final int FLING_MIN_VELOCITY = 200;

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

??????? mGestureDetector = new GestureDetector(this);

??????? TextView tv = (TextView) findViewById(R.id.page);
??????? tv.setOnTouchListener(this);
??????? tv.setText(R.string.text);
??????? tv.setLongClickable(true);
??? }

??? @Override
??? public boolean onTouch(View v, MotionEvent event) {
??????? return mGestureDetector.onTouchEvent(event);
??? }
??? @Override
??? public boolean onDown(MotionEvent e) {
??????? return false;
??? }
??? @Override
??? public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
??????????? float velocityY) {
??????? if (e1.getX() – e2.getX() > FLING_MIN_DISTANCE
??????????????? && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
??????????? // Fling left
??????????? Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
??????? } else if (e2.getX() – e1.getX() > FLING_MIN_DISTANCE
??????????????? && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
??????????? // Fling right
??????????? Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
??????? }
??????? return false;
??? }
??? @Override
??? public void onLongPress(MotionEvent e) {

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

??? @Override
??? public void onShowPress(MotionEvent e) {

??? }
??? @Override
??? public boolean onSingleTapUp(MotionEvent e) {
??????? return false;
??? }

}

?

源代码:http://easymorse-android.googlecode.com/svn/trunk/GestureTest

热点排行