利用OnGestureListener识别用户手势

android可以识别用户的手势(即用户用手指滑动的方向),通过用户不同的手势,从而做出不同的处理,下面是一个识别用户手势的效果:

image

 

实现过程是,继承OnGestureListener方法,通过坐标来实现。下面是主要代码:

public class GestureTest extends Activity implements OnTouchListener, 
        OnGestureListener {

    GestureDetector mGestureDetector; 
    private static final int FLING_MIN_DISTANCE = 100; 
    private static final int FLING_MIN_VELOCITY = 200;

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);

        mGestureDetector = new GestureDetector(this);

        TextView tv = (TextView) findViewById(R.id.page); 
        tv.setOnTouchListener(this); 
        tv.setText(R.string.text); 
        tv.setLongClickable(true); 
    }

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
        return mGestureDetector.onTouchEvent(event); 
    } 
    @Override 
    public boolean onDown(MotionEvent e) { 
        return false; 
    } 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
            float velocityY) { 
        if (e1.getX() – e2.getX() > FLING_MIN_DISTANCE 
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) { 
            // Fling left 
            Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show(); 
        } else if (e2.getX() – e1.getX() > FLING_MIN_DISTANCE 
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) { 
            // Fling right 
            Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show(); 
        } 
        return false; 
    } 
    @Override 
    public void onLongPress(MotionEvent e) {

    } 
    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
            float distanceY) { 
        return false; 
    }

    @Override 
    public void onShowPress(MotionEvent e) {

    } 
    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
        return false; 
    }

}

源代码:http://easymorse-android.googlecode.com/svn/trunk/GestureTest

本文引用:http://www.williamhua.com/2009/04/23/android-touchscreen-gesture-recogniton/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值