android使用gesturedetector手势识别示例分享

android使用gesturedetector手势识别示例分享,第1张

概述复制代码代码如下:publicclassMyGestureLintenerextendsSimpleOnGestureListener{privateContextcontext;publicMyGestureLintener(Contextcontext){   super();   this.context=conte

复制代码 代码如下:
public class MyGesturelintener extends SimpleOnGestureListener {
private Context context;
public MyGesturelintener(Context context) {
    super();
    this.context = context;
}

// 单击,触摸屏按下时立刻触发
/*@OverrIDe
public boolean onDown(MotionEvent e) {
    // Todo auto-generated method stub
    Toast.makeText(context,"Down " + e.getAction(),Toast.LENGTH_SHORT)
        .show();
    return true;
}*/
// 双击,手指在触摸屏上迅速点击第二下时触发
@OverrIDe
public boolean onDoubleTap(MotionEvent e) {
    // Todo auto-generated method stub
    return super.onDoubleTap(e);
}

// 双击的按下跟抬起各触发一次
@OverrIDe
public boolean onDoubleTapEvent(MotionEvent e) {
    // Todo auto-generated method stub
    return super.onDoubleTapEvent(e);
}

 

// 滑动,触摸屏按下后快速移动并抬起,会先触发滚动手势,跟着触发一个滑动手势
@OverrIDe
public boolean onFling(MotionEvent e1,MotionEvent e2,float veLocityX,
        float veLocityY) {
    // Todo auto-generated method stub
    return super.onFling(e1,e2,veLocityX,veLocityY);
}

// 长按,触摸屏按下后既不抬起也不移动,过一段时间后触发
@OverrIDe
public voID onLongPress(MotionEvent e) {
    // Todo auto-generated method stub
    Toast.makeText(context,"LONG " + e.getAction(),Toast.LENGTH_SHORT)
            .show();
}

// 滚动,触摸屏按下后移动
@OverrIDe
public boolean onScroll(MotionEvent e1,float distanceX,
        float distanceY) {
    Toast.makeText(context,"onScroll " + e2.getAction(),Toast.LENGTH_SHORT)
    .show();
    return true;
}

// 短按,触摸屏按下后片刻后抬起,会触发这个手势,如果迅速抬起则不会
@OverrIDe
public voID onShowPress(MotionEvent e) {
    // Todo auto-generated method stub
    Toast.makeText(context,"Show " + e.getAction(),Toast.LENGTH_SHORT)
            .show();

}

// 单击确认,即很快的按下并抬起,但并不连续点击第二下
/*@OverrIDe
public boolean onSingleTapConfirmed(MotionEvent e) {
    // Todo auto-generated method stub
    Toast.makeText(context,"onSingleTapConfirmed " + e.getAction(),Toast.LENGTH_SHORT)
    .show();
    return true;
}*/

// 抬起,手指离开触摸屏时触发(长按、滚动、滑动时,不会触发这个手势)
/*@OverrIDe
public boolean onSingleTapUp(MotionEvent e) {
    // Todo auto-generated method stub

    Toast.makeText(context,"onSingleTapUp " + e.getAction(),Toast.LENGTH_SHORT)
    .show();
    return true;
}*/
public class MainActivity extends Activity {
private GestureDetector mGestureDetector;//手势对象
private MyGesturelintener myGesturelintener;//手势监听的接口对象

@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentVIEw(R.layout.activity_main);
    myGesturelintener = new MyGesturelintener(this);

    //手势对象的构造方法
    mGestureDetector = new GestureDetector(this,
            myGesturelintener);
}

/**GestureDetector类的ontouchEvent方法用来辨别不同的手势*/
@OverrIDe
public boolean ontouchEvent(MotionEvent event) {
    boolean b = false;
    int i = event.getAction();
    int j = MotionEvent.ACTION_MOVE;
    System.out.println(i+"<----------------->"+j);
    b = mGestureDetector.ontouchEvent(event);
    if (b) {
        Intent in = new Intent();
        in.setClass(this,testActivity.class);
        startActivity(in);
    }
    return b;

}

@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;
}
}

总结

以上是内存溢出为你收集整理的android使用gesturedetector手势识别示例分享全部内容,希望文章能够帮你解决android使用gesturedetector手势识别示例分享所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1141627.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存