android GestureDetector應用

來源:互聯網
上載者:User

直接上代碼了: [java]  import android.app.Activity;  import android.os.Bundle;  import android.util.Log;  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.TextView;  import android.widget.Toast;    public class GestureActivity extends Activity implements OnTouchListener,          OnGestureListener {        GestureDetector detector;        public GestureActivity() {          detector = new GestureDetector(this);      }            public void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);                setContentView(R.layout.main);                TextView tv = (TextView) findViewById(R.id.TextView001);              //設定tv的******                tv.setOnTouchListener(this);                tv.setFocusable(true);              //必須,view才能夠處理不同於Tap(輕觸)的hold              tv.setClickable(true);                tv.setLongClickable(true);                detector.setIsLongpressEnabled(true);        }                    /*       * 在onTouch()方法中,我們調用GestureDetector的onTouchEvent()方法,將捕捉到的MotionEvent交給GestureDetector       * 來分析是否有合適的callback函數來處理使用者的手勢       */         public boolean onTouch(View v, MotionEvent event) {            return detector.onTouchEvent(event);        }            // 使用者輕觸觸控螢幕,由1個MotionEvent ACTION_DOWN觸發        public boolean onDown(MotionEvent arg0) {            Log.i("MyGesture", "onDown");            Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();            return true;        }                /*       * 使用者輕觸觸控螢幕,尚未鬆開或拖動,由一個1個MotionEvent ACTION_DOWN觸發        * 注意和onDown()的區別,強調的是沒有鬆開或者拖動的狀態       */        public void onShowPress(MotionEvent e) {            Log.i("MyGesture", "onShowPress");            Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();        }                // 使用者(輕觸觸控螢幕後)鬆開,由一個1個MotionEvent ACTION_UP觸發        public boolean onSingleTapUp(MotionEvent e) {            Log.i("MyGesture", "onSingleTapUp");            Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();            return true;        }                // 使用者按下觸控螢幕、快速移動後鬆開,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE, 1個ACTION_UP觸發        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {            Log.i("MyGesture", "onFling");                      // 參數解釋:            // e1:第1個ACTION_DOWN MotionEvent            // e2:最後一個ACTION_MOVE MotionEvent            // velocityX:X軸上的移動速度,像素/秒            // velocityY:Y軸上的移動速度,像素/秒                    // 觸發條件 :            // X軸的座標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒                        final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;            if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {                // Fling left                Log.i("MyGesture", "Fling left");                Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();            } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {                // Fling right                Log.i("MyGesture", "Fling right");                Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();            } else if(e2.getY()-e1.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {              // Fling down                Log.i("MyGesture", "Fling down");                Toast.makeText(this, "Fling down", Toast.LENGTH_SHORT).show();          } else if(e1.getY()-e2.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {              // Fling up                Log.i("MyGesture", "Fling up");                Toast.makeText(this, "Fling up", Toast.LENGTH_SHORT).show();          }                                return false;                  }                // 使用者按下觸控螢幕,並拖動,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE觸發        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {            Log.i("MyGesture", "onScroll");            Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show();            return true;        }                // 使用者長按觸控螢幕,由多個MotionEvent ACTION_DOWN觸發        public void onLongPress(MotionEvent e) {            Log.i("MyGesture", "onLongPress");            Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show();        }            }   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.