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;   
        }   
    }  
  • 相关阅读:
    MySQL语句创建表、插入数据遇到的问题-20/4/18
    【Navicat】MySQL 8.0.17 数据库报2059错误
    MySQL 8.0.17 版安装 || Windows
    C#--Invoke和BeginInvoke用法和区别
    C#--params关键字
    C#--typeof() 和 GetType()区别
    C#--利用反射编写的SqlHelper类
    C#--反射基础
    C#--LINQ--2--LINQ高级查询
    C#--LINQ--1--初学LINQ基础和查询
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/6438953.html
Copyright © 2011-2022 走看看