zoukankan      html  css  js  c++  java
  • Android OnGestureListener用法 识别用户手势 左右滑动

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

    需要使用OnGestureListener

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

    直接上代码

    界面文件

    main.xml

    <?xml version="1.0" encoding="utf-8"?>   
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:id="@+id/ll"  
        >   
    <TextView     
           
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"    
        android:text="@string/hello"  
        />   
    </LinearLayout>  
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/ll"
        >
    <TextView  
        
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    </LinearLayout>

    主Activity

    package zy.lucifer.testGesture;   
    import android.app.Activity;   
    import android.os.Bundle;   
    import android.util.Log;   
    import android.view.GestureDetector;   
    import android.view.MotionEvent;   
    import android.view.View;   
    import android.view.GestureDetector.OnGestureListener;   
    import android.view.View.OnTouchListener;   
    import android.widget.LinearLayout;   
    import android.widget.TextView;   
    import android.widget.Toast;   
    public class testGesture extends Activity implements OnTouchListener,   
            OnGestureListener {   
        GestureDetector mGestureDetector;   
        private static final int FLING_MIN_DISTANCE = 50;   
        private static final int FLING_MIN_VELOCITY = 0;   
        /** Called when the activity is first created. */  
        @Override   
        public void onCreate(Bundle savedInstanceState) {   
            super.onCreate(savedInstanceState);   
            setContentView(R.layout.main);   
            mGestureDetector = new GestureDetector(this);   
            LinearLayout ll=(LinearLayout)findViewById(R.id.ll);   
            ll.setOnTouchListener(this);   
            ll.setLongClickable(true);   
        }   
        @Override   
        public boolean onTouch(View v, MotionEvent event) {   
            // TODO Auto-generated method stub   
            Log.i("touch","touch");   
             return mGestureDetector.onTouchEvent(event);    
        }   
        @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   
             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) {   
            // TODO Auto-generated method stub   
        }   
        @Override   
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,   
                float distanceY) {   
            // TODO Auto-generated method stub   
            return false;   
        }   
        @Override   
        public void onShowPress(MotionEvent e) {   
            // TODO Auto-generated method stub   
        }   
        @Override   
        public boolean onSingleTapUp(MotionEvent e) {   
            // TODO Auto-generated method stub   
            return false;   
        }   
    }  
  • 相关阅读:
    hdu5587 BestCoder Round #64 (div.2)
    hdu5569 BestCoder Round #63 (div.2)
    hihocoder1257(构造)(2015北京ACM/ICPC)
    hihocoder 1249(2015ACM/ICPC北京)
    hihocoder1258(水)(2015ACM/ICPC北京站)
    hihiocoder 1255(搜索)(2015ACM/ICPC北京站)
    习题9-8 uva1631
    习题9-8 Uva1632
    Orz
    习题9-6 uva 10723
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/6438953.html
Copyright © 2011-2022 走看看