zoukankan      html  css  js  c++  java
  • Android_GestureDetector

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.android_gesturedetector.MainActivity" >
        <ImageView 
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher"/>
    </RelativeLayout>

    源代码:

    package com.example.android_gesturedetector;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.GestureDetector;
    import android.view.GestureDetector.SimpleOnGestureListener;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.ImageView;
    import android.widget.Toast;
    /*
     * 使用GestureDetector进行手势识别
     * 手势交互过程(原理):
     * 1.触屏一刹那,触发MotionEvent事件(移动事件)
     * 2.被OnTouchListener监听,在onTouch()中获得MotionEvent对象
     * 3.GestureDetector(手势识别器)转发MotionEvent对象至OnGestureListener
     * 4.OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈
     * 
     * 
     * 
     * MotionEvent:
     * 1.用于封装手势,触摸笔,轨迹球等动作事件
     * 2.内部封装用于记录横轴和纵轴坐标的属性x和y
     * 
     * GestureDetector:(手势识别器)
     * 识别各种手势
     *     工作原理:
     *     1.当接收到用户触摸消息时 ,将消息交给GestureDetector加工
     *     2.通过设置监听器获得GestureDetector处理后的手势
     * GestureDetector提供了两个接口
     *     OnGestureListener:(是被单击事件)
     *         1.手势交互的监听接口,其提供多个抽象方法
     *         2.根据GestureDetector的手势识别结果调用相对应的方法
     *     OnDoubleTapListener.
     * SimpleOnGeistureListener实现了上面两种接口
     *     1.继承SimpleOnGestureListener
     *     2.可以只重载感兴趣的手势
     */
    public class MainActivity extends Activity {
        private ImageView image;
        private GestureDetector myGestureDetector;
        class MyGestureListener extends SimpleOnGestureListener{
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                // TODO Auto-generated method stub
                if(e1.getX()-e2.getX()>50){
                Toast.makeText(MainActivity.this, "从右往左滑", Toast.LENGTH_SHORT).show();
                }else if(e2.getX()-e1.getX()>50){
                    Toast.makeText(MainActivity.this,"从左往右滑",Toast.LENGTH_SHORT).show();
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        }
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            image = (ImageView) findViewById(R.id.image);
            myGestureDetector = new GestureDetector(new MyGestureListener());//实例化GestureDetector
            image.setOnTouchListener(new OnTouchListener() {
                
                @Override//可以捕获到触摸屏幕发生的event事件
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    myGestureDetector.onTouchEvent(event);//GestureDetector对象通过onTouchEvent方法将event事件转发给GestureListener接口的实现类
                    return true;
                }
            });
        
        }
    
    }
  • 相关阅读:
    shell
    regionMatches方法
    ==
    使用INTO子句创建新表
    数据库除运算
    数据库笛卡尔积运算
    人生格言
    刚开通~
    Nginx:413 Request Entity Too Large
    ORACLE 查看并修改最大连接数
  • 原文地址:https://www.cnblogs.com/fangg/p/5579620.html
Copyright © 2011-2022 走看看