android可以识别用户的手势

来源:互联网 发布:sql注入教程完整版 编辑:程序博客网 时间:2024/06/03 07:51

android可以识别用户的手势(即用户用手指滑动的方向),通过用户不同的手势,从而做出不同的处理

需要使用OnGestureListener

比如说看电子书的时候翻页,或者要滑动一些其他内容

直接上代码

界面文件

main.xml

 

view plaincopy to clipboardprint?

1.  <?xml version="1.0" encoding="utf-8"?>   

2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.      android:orientation="vertical"  

4.      android:layout_width="fill_parent"  

5.      android:layout_height="fill_parent"  

6.      android:id="@+id/ll"  

7.      >   

8.  <TextView     

9.         

10.     android:layout_width="fill_parent"    

11.     android:layout_height="wrap_content"    

12.     android:text="@string/hello"  

13.     />   

14. </LinearLayout>  

 

Activity

 

view plaincopy to clipboardprint?

1.  package zy.lucifer.testGesture;   

2.  import android.app.Activity;   

3.  import android.os.Bundle;   

4.  import android.util.Log;   

5.  import android.view.GestureDetector;   

6.  import android.view.MotionEvent;   

7.  import android.view.View;   

8.  import android.view.GestureDetector.OnGestureListener;   

9.  import android.view.View.OnTouchListener;   

10. import android.widget.LinearLayout;   

11. import android.widget.TextView;   

12. import android.widget.Toast;   

13. public class testGesture extends Activity implements OnTouchListener,   

14.         OnGestureListener {   

15.     GestureDetector mGestureDetector;   

16.     private static final int FLING_MIN_DISTANCE = 50;   

17.     private static final int FLING_MIN_VELOCITY = 0;   

18.     /** Called when the activity is first created. */  

19.     @Override   

20.     public void onCreate(Bundle savedInstanceState) {   

21.         super.onCreate(savedInstanceState);   

22.         setContentView(R.layout.main);   

23.         mGestureDetector = new GestureDetector(this);   

24.         LinearLayout ll=(LinearLayout)findViewById(R.id.ll);   

25.         ll.setOnTouchListener(this);   

26.         ll.setLongClickable(true);   

27.     }   

28.     @Override   

29.     public boolean onTouch(View v, MotionEvent event) {   

30.         // TODO Auto-generated method stub   

31.         Log.i("touch","touch");   

32.          return mGestureDetector.onTouchEvent(event);    

33.     }   

34.     @Override   

35.     public boolean onDown(MotionEvent e) {   

36.         // TODO Auto-generated method stub   

37.         return false;   

38.     }   

39.     @Override   

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

41.             float velocityY) {   

42.         // TODO Auto-generated method stub   

43.          if (e1.getX()-e2.getX() > FLING_MIN_DISTANCE    

44.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    

45.                 // Fling left    

46.                 Toast.makeText(this"向左手势", Toast.LENGTH_SHORT).show();    

47.             } else if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE    

48.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    

49.                 // Fling right    

50.                 Toast.makeText(this"向右手势", Toast.LENGTH_SHORT).show();    

51.             }    

52.             return false;    

53.     }   

54.     @Override   

55.     public void onLongPress(MotionEvent e) {   

56.         // TODO Auto-generated method stub   

57.     }   

58.     @Override   

59.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,   

60.             float distanceY) {   

61.         // TODO Auto-generated method stub   

62.         return false;   

63.     }   

64.     @Override   

65.     public void onShowPress(MotionEvent e) {   

66.         // TODO Auto-generated method stub   

67.     }   

68.     @Override   

69.     public boolean onSingleTapUp(MotionEvent e) {   

70.         // TODO Auto-generated method stub   

71.         return false;   

72.     }   

73. }  

 

原创粉丝点击