编写 android 简单的手势切换视图示例
2010-04-13 01:57:00 来源:WEB开发网覆盖Activity的onTouchEvent方法,在方法内部,调用GestureDetector实例的onTouchEvent方法。
代码:
package com.easymorse;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.ViewFlipper;
public class MainActivity extends Activity implements
OnGestureListener {
private ViewFlipper flipper;
private GestureDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this);
flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.v("golf", "touched");
return this.detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i("golf", "fling…");
if (e1.getX() > e2.getX()) {
this.flipper.showNext();
} else if (e1.getX() < e2.getX()) {
this.flipper.showPrevious();
} else {
return false;
}
return true;
}
@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;
}
}
赞助商链接