Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 手勢操作GestureDetector

android 手勢操作GestureDetector

編輯:關於Android編程

package com.sphere.gesturetest;

import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends Activity implements OnGestureListener{

    GestureDetector gestureDetector;
    private int MIN_DESTANCE = 50;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetector(this,this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //交給手勢檢測類處理touch事件
        return gestureDetector.onTouchEvent(event);
    }

    //用戶按下時觸發
    @Override
    public boolean onDown(MotionEvent arg0) {
        // TODO Auto-generated method stub
        toast(按下);
        return false;
    }

    //用戶在屏幕上拖動時觸發
    @Override
    public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // TODO Auto-generated method stub
        if(arg0.getX() - arg1.getX() > MIN_DESTANCE){
            toast(向左滑);
        }
        if(arg0.getX() - arg1.getX() < MIN_DESTANCE){
            toast(向右滑);
        }
        return false;
    }

    @Override
    public void onLongPress(MotionEvent arg0) {
        // TODO Auto-generated method stub
        toast(長按);
    }

    @Override
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // TODO Auto-generated method stub
        //toast(滾動。。。。);
        return false;
    }

    //當用戶在屏幕上按下 而且還未移動和松開時觸發
    @Override
    public void onShowPress(MotionEvent arg0) {
        // TODO Auto-generated method stub
        toast(onShowPress。。。。);
    }

    //用戶在觸摸屏上輕擊事件將會觸發
    @Override
    public boolean onSingleTapUp(MotionEvent arg0) {
        // TODO Auto-generated method stub
        toast(onSingleTapUp。。。。);
        return false;
    }
    
    public void toast(String text){
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

}

 

 

 

 

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