DFdou's Blog Life is short,Be yourself.

1401/100

Android-OnGestureListener接口

在做应用的时候难免会碰到触摸事件,Android提供了不少事件,只要实现OnGestureListener接口,并且呢,在onTouch方法里写个:

//记得要implements OnTouchListener,OnGestureListener
//public class mGestureDetector extends Activity implements OnTouchListener,OnGestureListener
private GestureDetector mGestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	// setContentView(R.layout.main);
	View v = new View(this);
	setContentView(v);
        //mGestureDetector是个GestureDetector对象,记得初始化一下。
	mGestureDetector = new GestureDetector(this);
        //另外呢,需要针对onFling方法的监听,需要在监听的View里边加上:
	v.setOnTouchListener(this);
	v.setLongClickable(true);
}
public boolean onTouch(View v, MotionEvent mo) {
	return mGestureDetector.onTouchEvent(mo);
}

另外是OnGestureListener接口必须实现的几个方法,具体要怎么实现大家自个儿对号入座吧。

public boolean onDown(MotionEvent e) {
	return false;
}
public void onLongPress(MotionEvent e) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
    float distanceY) {
	return false;
}
public void onShowPress(MotionEvent e) {
}
public boolean onSingleTapConfirmed(MotionEvent e) {
	return false;
}
public boolean onSingleTapUp(MotionEvent e) {
	return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
	return true;
}

Some Related Posts

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

:zhuang :xizao :wuwu :wenhao :wc :touxiang :shuaya :shuajian :shengtian :paopao :no :meinv :maonv :loveu :leilei :kua :jrjr :dahan :chi :chaocai :byebye


CommentLuv Enabled

No trackbacks yet.