GestureDetector手勢(shì)監(jiān)聽

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;

/**
 * 手勢(shì)監(jiān)聽
 * 
 * @author admin
 * @時(shí)間 2014-11-13上午10:00:22
 */
public class MainActivity extends Activity implements OnGestureListener,
        OnTouchListener {

    @SuppressWarnings("deprecation")
    /**
     * 創(chuàng)建一個(gè)用于識(shí)別手勢(shì)的GestureDetector的對(duì)象
     */
    private GestureDetector detector = new GestureDetector(this);
    /**
     * 創(chuàng)建一個(gè)布局,這里指主頁面布局
     */
    private RelativeLayout rLayout;

    /**
     * 限制最小移動(dòng)像素
     */
    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);
        // 綁定監(jiān)聽
        rLayout.setOnTouchListener(this);

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

    }

    /**
     * 按下時(shí)被調(diào)用
     */
    @Override
    public boolean onDown(MotionEvent e) {
        // Toast.makeText(this, "按下了", 0).show();
        return false;
    }

    /**
     * 按住時(shí)被調(diào)用
     */
    @Override
    public void onShowPress(MotionEvent e) {
        Toast.makeText(this, "按住了", 0).show();

    }

    /**
     * 抬起時(shí)被調(diào)用(如果長(zhǎng)按了就不會(huì)調(diào)用此方法)
     */
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Toast.makeText(this, "抬起了", 0).show();
        return false;
    }

    /**
     * 滾動(dòng)時(shí)被調(diào)用
     */
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        Toast.makeText(this, "滾動(dòng)了", 0).show();
        return false;
    }

    /**
     * 長(zhǎng)按時(shí)被調(diào)用
     */
    @Override
    public void onLongPress(MotionEvent e) {
        Toast.makeText(this, "長(zhǎng)按了", 0).show();
        iv.setVisibility(View.VISIBLE);
    }

    /**
     * 手勢(shì)滑動(dòng)時(shí)調(diào)用
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // X軸的坐標(biāo)位移大于FLING_MIN_DISTANCE,且移動(dòng)速度大于FLING_MIN_VELOCITY個(gè)像素/秒
        if (e1.getX() - e2.getX() > FLING_MIX_DISTANCE) {
            Toast.makeText(this, "左滑動(dòng)", 0).show();
        }
        if (e2.getX() - e1.getX() > FLING_MIX_DISTANCE) {
            Toast.makeText(this, "右滑動(dòng)", 0).show();
        }
        return false;
    }

    /**
     * 重寫OnTouchListener的onTouch方法 此方法在觸摸屏被觸摸,即發(fā)生觸摸事件(接觸和撫摸兩個(gè)事件)的時(shí)候被調(diào)用
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // 這相當(dāng)于給手機(jī)綁定手勢(shì)偵聽器
        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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容