Android OnTouchListener 触摸事件 & GestureDetector 手势识别类

OnTouchListener 触摸事件 & GestureDetector 手势识别类

package com.example.gesturedetector;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

/*
 * GestureDetector 手势识别类
 */

public class MainActivity extends Activity {

    private ImageView img0;
    private GestureDetector myGestureDetector;

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

        myGestureDetector = new GestureDetector(this, new MyOnGestureListener());

        img0 = (ImageView) findViewById(R.id.img0);
        img0.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e("AAA", "abc");
                myGestureDetector.onTouchEvent(event);
                return true;
            }
        });
    }

    class MyOnGestureListener extends SimpleOnGestureListener{
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Log.e("AAA", "轻击屏幕-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Log.e("AAA", "长按触摸屏-----" + getActionName(e.getAction()));
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Log.e("AAA","拖动屏幕-----" + getActionName(e2.getAction()) + ",(" + e1.getX() + "," + e1.getY() + ") ,("+ e2.getX() + "," + e2.getY() + ")");
            return false;
        }

        // e1 代表手指第一次触摸屏幕的事件
        // e2 代表手指离开屏幕一瞬间的事件
        // velocityX 水平方向的速度 单位 pix/s
        // velocityY 竖直方向的速度
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.e("AAA","滑屏-----" + getActionName(e2.getAction()) + ",(" + e1.getX() + "," + e1.getY() + ") ,("+ e2.getX() + "," + e2.getY() + ")");
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            Log.e("AAA", "按下短时间-----" + getActionName(e.getAction()));
        }

        @Override
        public boolean onDown(MotionEvent e) {
            Log.e("AAA", "按下屏幕触发-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.e("AAA", "双击的第二下-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            Log.e("AAA","双击的手势中的事件down、up和move-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.e("AAA", "用来判定该次点击是SingleTap而不是DoubleTap-----" + getActionName(e.getAction()));
            return false;
        }
    }

    private String getActionName(int action) {
        String name = "";
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                name = "ACTION_DOWN";
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                name = "ACTION_MOVE";
                break;
            }
            case MotionEvent.ACTION_UP: {
                name = "ACTION_UP";
                break;
            }
            default:
            break;
        }
        return name;
    }
}

你可能感兴趣的:(Android,Basics)