android 手勢操作GestureDetector

來源:互聯網
上載者:User

標籤:android   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();    }}

其他精彩文章文章

在 android dialog中使用Autocompletetext 
大型網站架構設計-Solr
mysql雜湊索引
android學習筆記(32)網格視圖(GridView )和圖形切換器(ImageSwi...
android學習筆記(31)可展開的列表組件(ExpandableListView )

更多關於android開發文章


android 手勢操作GestureDetector

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.