如何通过GestureDetector实现屏幕事件监听
展开全部
内容如题:
1.SlideExample.java文件
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewGroup;
public class SlideExample extends Activity {
public ViewGroup container1, container2;
private GestureDetector gestureDetector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 监听屏幕动作事件
SampleGuest gestureListener = new SampleGuest(this);
gestureDetector = new GestureDetector(gestureListener);
container1 = (ViewGroup) findViewById(R.id.container1);
container2 = (ViewGroup) findViewById(R.id.container2);
}
// called automatically, any screen action will Triggered it
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
}
2.SampleGuest.java文件
package com.example;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.AnimationUtils;
public class SampleGuest implements OnGestureListener {
SlideExample se;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_MIN_DISTANCE = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
public SampleGuest(SlideExample se) {
this.se = se;
}
// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
public boolean onDown(MotionEvent e) {
Log.d("TAG", "[onDown]");
return true;
}
1.SlideExample.java文件
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewGroup;
public class SlideExample extends Activity {
public ViewGroup container1, container2;
private GestureDetector gestureDetector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 监听屏幕动作事件
SampleGuest gestureListener = new SampleGuest(this);
gestureDetector = new GestureDetector(gestureListener);
container1 = (ViewGroup) findViewById(R.id.container1);
container2 = (ViewGroup) findViewById(R.id.container2);
}
// called automatically, any screen action will Triggered it
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
}
2.SampleGuest.java文件
package com.example;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.AnimationUtils;
public class SampleGuest implements OnGestureListener {
SlideExample se;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_MIN_DISTANCE = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
public SampleGuest(SlideExample se) {
this.se = se;
}
// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
public boolean onDown(MotionEvent e) {
Log.d("TAG", "[onDown]");
return true;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询