码迷,mamicode.com
首页 > 移动开发 > 详细

Android的手势

时间:2016-05-07 08:51:08      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.testdectorgestor;

import android.support.v7.app.ActionBarActivity;
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.widget.TextView;
import android.widget.Toast;
/**
 * 使用步骤:
 *     1 新建一个类继承自SimpleOnGestureListen,重写需要重写重写的方法
 *     2 注册拦截手势触发事件器tv.setOnTouchListener(this);
 *     3 根据业务逻辑来进行处理
 */
public class ThirdActivity extends ActionBarActivity implements OnTouchListener {
    private GestureDetector detector;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        detector = new GestureDetector(ThirdActivity.this,
                new MyGestureListener());
        tv = (TextView) findViewById(R.id.tv);
        tv.setOnTouchListener(this);// 注册拦截手势触发事件器
        tv.setClickable(true);
        tv.setLongClickable(true);
    }

    // 拦截OnGestureListener触发的事件
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        return detector.onTouchEvent(arg1);
    }

    private class MyGestureListener extends SimpleOnGestureListener {

        // 用户按下屏幕触发
        @Override
        public boolean onDown(MotionEvent e) {

            Log.e("MyGestureListener", "onDown");
            Toast.makeText(ThirdActivity.this, "onDown", 0).show();
            return super.onDown(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.e("MyGestureListener", "onDoubleTap");
            Toast.makeText(ThirdActivity.this, "onDoubleTap", 0).show();
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            Log.e("MyGestureListener", "onDoubleTapEvent");
            Toast.makeText(ThirdActivity.this, "onDoubleTapEvent", 0).show();
            return super.onDoubleTapEvent(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            /**
             * 滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE,1个ACTION_UP触发
             * 参数解释: e1:第1个ACTION_DOWN MotionEvent
             *        e2:最后一个ACTION_MOVE MotionEvent 
             *        velocityX:X轴上的移动速度,像素/秒
             *        velocityY:Y轴上的移动速度,像素/秒
             */
            Log.e("MyGestureListener", "onFling");
            Toast.makeText(ThirdActivity.this, "onFling", 0).show();
            return super.onFling(e1, e2, velocityX, velocityY);
        }

        // 用户长按屏幕触发
        @Override
        public void onLongPress(MotionEvent e) {
            Log.e("MyGestureListener", "onLongPress");
            Toast.makeText(ThirdActivity.this, "onLongPress", 0).show();
            super.onLongPress(e);
        }

        // 用户滑动屏幕触发
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            /**onDown-----》onScroll----》onScroll----》onScroll----》………----->onFling
             * 滑动最后触发了onFling
             */
            Log.e("MyGestureListener", "onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);
            Toast.makeText(ThirdActivity.this, "onScroll", 0).show();
            return super.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public void onShowPress(MotionEvent e) {
            Log.e("MyGestureListener", "onShowPress");
            Toast.makeText(ThirdActivity.this, "onShowPress", 0).show();
            super.onShowPress(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.e("MyGestureListener", "onSingleTapConfirmed");
            Toast.makeText(ThirdActivity.this, "onSingleTapConfirmed", 0)
                    .show();
            return super.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Log.e("MyGestureListener", "onSingleTapUp");
            Toast.makeText(ThirdActivity.this, "onSingleTapUp", 0).show();
            return super.onSingleTapUp(e);
        }

    }

}
<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.testdectorgestor.SecondActivity" >

    <TextView
        android:id="@+id/tv"
        android:background="#000000"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:textColor="#ffffff"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:text="@string/hello_world" />

</RelativeLayout>

Android的手势

标签:

原文地址:http://blog.csdn.net/wnl_csdn/article/details/51335468

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!