WEB开发网
开发学院手机开发Android 开发 编写 android 简单的手势切换视图示例 阅读

编写 android 简单的手势切换视图示例

 2010-04-13 01:57:00 来源:WEB开发网   
核心提示:覆盖Activity的onTouchEvent方法,在方法内部,编写 android 简单的手势切换视图示例(2),调用GestureDetector实例的onTouchEvent方法,代码:package com.easymorse;import android.app.Activity;import android

覆盖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;

}

}

上一页  1 2 

Tags:编写 android 简单

编辑录入:coldstar [复制链接] [打 印]
赞助商链接