码迷,mamicode.com
首页 > 移动开发 > 详细

android 手势操作GestureDetector

时间:2015-01-08 18:01:07      阅读:189      评论:0      收藏:0      [点我收藏+]

标签: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

标签:android   android开发   

原文地址:http://blog.csdn.net/androidmylove/article/details/42527009

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!