Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android學習 多讀官網,有益健康---手勢

Android學習 多讀官網,有益健康---手勢

編輯:關於Android編程

 

一、可以直接覆蓋Activity的onTouch方法

 

public class MainActivity extends Activity {
...
// This example shows an Activity, but you would use the same approach if
// you were subclassing a View.
@Override
public boolean onTouchEvent(MotionEvent event){ 
        
    int action = MotionEventCompat.getActionMasked(event);
        
    switch(action) {
        case (MotionEvent.ACTION_DOWN) :
            Log.d(DEBUG_TAG,Action was DOWN);
            return true;
        case (MotionEvent.ACTION_MOVE) :
            Log.d(DEBUG_TAG,Action was MOVE);
            return true;
        case (MotionEvent.ACTION_UP) :
            Log.d(DEBUG_TAG,Action was UP);
            return true;
        case (MotionEvent.ACTION_CANCEL) :
            Log.d(DEBUG_TAG,Action was CANCEL);
            return true;
        case (MotionEvent.ACTION_OUTSIDE) :
            Log.d(DEBUG_TAG,Movement occurred outside bounds  +
                    of current screen element);
            return true;      
        default : 
            return super.onTouchEvent(event);
    }      
}

 

二:也可以給單個的view設置監聽器

View myView = findViewById(R.id.my_view); 
myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        // ... Respond to touch events       
        return true;
    }
});

三:但是 onTouch方法畢竟只能檢測一些簡單的手勢,像滑動、雙擊、長按等,單用onTouch方法處理就顯得棘手了,谷歌提供了方便的 GestureDetector 類來更方便的處理手勢。

具體的需要實例 GestureDetectorCompat 類, 該類中的構造方法需要實現 監聽器。GestureDetector.OnGestureListener 去通知用戶,當各種事件(滑動、雙擊、長按等)發生。為了使 GestureDetector 正常監聽事件,同時需要重寫Activity的 nTouch方法,把事件交給Detector。具體見官網demo:

 

public class MainActivity extends Activity implements 
        GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener{
    
    private static final String DEBUG_TAG = Gestures;
    private GestureDetectorCompat mDetector; 

    // Called when the activity is first created. 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Instantiate the gesture detector with the
        // application context and an implementation of
        // GestureDetector.OnGestureListener
        mDetector = new GestureDetectorCompat(this,this);
        // Set the gesture detector as the double tap
        // listener.
        mDetector.setOnDoubleTapListener(this);
    }

    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
        this.mDetector.onTouchEvent(event);
        // Be sure to call the superclass implementation
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent event) { 
        Log.d(DEBUG_TAG,onDown:  + event.toString()); 
        return true;
    }

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2, 
            float velocityX, float velocityY) {
        Log.d(DEBUG_TAG, onFling:  + event1.toString()+event2.toString());
        return true;
    }

    @Override
    public void onLongPress(MotionEvent event) {
        Log.d(DEBUG_TAG, onLongPress:  + event.toString()); 
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        Log.d(DEBUG_TAG, onScroll:  + e1.toString()+e2.toString());
        return true;
    }

    @Override
    public void onShowPress(MotionEvent event) {
        Log.d(DEBUG_TAG, onShowPress:  + event.toString());
    }

    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        Log.d(DEBUG_TAG, onSingleTapUp:  + event.toString());
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent event) {
        Log.d(DEBUG_TAG, onDoubleTap:  + event.toString());
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent event) {
        Log.d(DEBUG_TAG, onDoubleTapEvent:  + event.toString());
        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
        Log.d(DEBUG_TAG, onSingleTapConfirmed:  + event.toString());
        return true;
    }
}

補充:
onScroll:在屏幕上慢慢的滾動。
onFling:在屏幕上快速的滾動,並快速移開手指。


Whether or not you use GestureDetector.OnGestureListener, it's best practice to implement an onDown()method that returns true. This is because all gestures begin with an onDown() message. If you return false fromonDown(), as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. This has the potential to cause unexpected problems in your app. The only time you should return false fromonDown() is if you truly want to ignore an entire gesture.

 

另外,官網上說最好復寫onDown方法,並返回true,因為其他方法的消息都是從onDown方法開始的。而且onDown系統默認返回false,其他的方法也不會被調用。

 

可是我自己試了下,當onDown方法返回false,其他方法是可以被調用的。。這就奇怪了。。。哪位高手親試過這個例子的,回復下。。

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