Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> android GestureDetector使用

android GestureDetector使用

編輯:Android開發實例

android為了增加用戶體驗,新增了GestureDetector類,那該類是做什麼用的呢?

通俗的講該類是 手勢檢測. 難道是手指有不同的動作系統會檢測到,是的.確實如此.

哎,問題來了,那詭異了.那這個類和touch有什麼關系呢?我們平常寫代碼的時候,如何是單擊,我們都是通過掛載個listener來響應這個櫥摸時間.那和這個類有關系嗎? 恩,有關系的.

touch 是觸摸,只要手指接觸到屏幕都是touch. 為了增加用戶體驗.手指在屏幕滑動算不算櫥摸呢?雙擊算不算櫥摸呢?都是啊.

所以說.  touch是個廣泛的概念,基本用來響應單擊事件,如果更細致的區分各中touch事件,那就需要該類了.

口說沒用,舉個例子最要緊,弄出APK是硬道理,來吧.


package com.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class Res extends Activity implements View.OnTouchListener {
    Button btn = null;
    private GestureDetector mGestureDetector = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.button);
        btn.setOnTouchListener(this);
        mGestureDetector = new GestureDetector(this, new LearnGestureListener());
    }

    public boolean onTouch(View view, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    class LearnGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent ev) {
            Log.d("DEBUG","onSingleTapUp");
            return true;
        }

        @Override
        public void onShowPress(MotionEvent ev) {
            Log.d("DEBUG","onShowPress");
        }

        @Override
        public void onLongPress(MotionEvent ev) {
            Log.d("DEBUG","onLongPress");
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            Log.d("DEBUG","onScroll");
            return true;
        }

        @Override
        public boolean onDown(MotionEvent ev) {
            Log.d("DEBUG","onDownd");
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            Log.d("DEBUG","onFling");
            return true;
        }
        public boolean onDoubleTap(MotionEvent event){
            Log.d("DEBUG","onDoubleTap");
            return true;
        }
    }

}

mGestureDetector = new GestureDetector(this, new LearnGestureListener());
        return mGestureDetector.onTouchEvent(event);

文件的關鍵基本就這兩句.第一句創建了對象,第二句把事件傳給這個對象.傳過去後系統回判斷做什麼動作.然後new LearnGestureListener()來調用相應的處理函數做處理.

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved