美文网首页
GestureDetector手势监听

GestureDetector手势监听

作者: lucas777 | 来源:发表于2022-05-29 21:39 被阅读0次
package com.xxx.gesturedetectordemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * 手势监听
 * 
 * @author admin
 * @时间 2014-11-13上午10:00:22
 */
public class MainActivity extends Activity implements OnGestureListener,
        OnTouchListener {

    @SuppressWarnings("deprecation")
    /**
     * 创建一个用于识别手势的GestureDetector的对象
     */
    private GestureDetector detector = new GestureDetector(this);
    /**
     * 创建一个布局,这里指主页面布局
     */
    private RelativeLayout rLayout;

    /**
     * 限制最小移动像素
     */
    private static final int FLING_MIX_DISTANCE = 110;

    private ImageView iv;

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

        rLayout = (RelativeLayout) findViewById(R.id.relativelayout);
        // 绑定监听
        rLayout.setOnTouchListener(this);

        iv = (ImageView) findViewById(R.id.iv);

    }

    /**
     * 按下时被调用
     */
    @Override
    public boolean onDown(MotionEvent e) {
        // Toast.makeText(this, "按下了", 0).show();
        return false;
    }

    /**
     * 按住时被调用
     */
    @Override
    public void onShowPress(MotionEvent e) {
        Toast.makeText(this, "按住了", 0).show();

    }

    /**
     * 抬起时被调用(如果长按了就不会调用此方法)
     */
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Toast.makeText(this, "抬起了", 0).show();
        return false;
    }

    /**
     * 滚动时被调用
     */
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        Toast.makeText(this, "滚动了", 0).show();
        return false;
    }

    /**
     * 长按时被调用
     */
    @Override
    public void onLongPress(MotionEvent e) {
        Toast.makeText(this, "长按了", 0).show();
        iv.setVisibility(View.VISIBLE);
    }

    /**
     * 手势滑动时调用
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
        if (e1.getX() - e2.getX() > FLING_MIX_DISTANCE) {
            Toast.makeText(this, "左滑动", 0).show();
        }
        if (e2.getX() - e1.getX() > FLING_MIX_DISTANCE) {
            Toast.makeText(this, "右滑动", 0).show();
        }
        return false;
    }

    /**
     * 重写OnTouchListener的onTouch方法 此方法在触摸屏被触摸,即发生触摸事件(接触和抚摸两个事件)的时候被调用
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // 这相当于给手机绑定手势侦听器
        detector.onTouchEvent(event);
        return true;
    }

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher"
        android:visibility="invisible"
        android:layout_centerVertical="true" />

</RelativeLayout>

相关文章

网友评论

      本文标题:GestureDetector手势监听

      本文链接:https://www.haomeiwen.com/subject/ycrnprtx.html