如何通过GestureDetector实现屏幕事件监听

 我来答
就烦条0o
2017-03-19 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46491
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
内容如题:
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个MotionEvent ACTION_DOWN,
// 多个ACTION_MOVE, 1个ACTION_UP触发
// e1:第1个ACTION_DOWN MotionEvent
// e2:最后一个ACTION_MOVE MotionEvent
// velocityX:X轴上的移动速度,像素/秒
// velocityY:Y轴上的移动速度,像素/秒
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if ((e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
se.container1.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_left_out));
se.container2.setVisibility(View.VISIBLE);
se.container2.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_right_in));
se.container1.setVisibility(View.GONE);
} else if ((e2.getX() - e1.getX()) > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
se.container1.setVisibility(View.VISIBLE);
se.container1.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_left_in));
se.container2.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_right_out));
se.container2.setVisibility(View.GONE);
}
return true;
}
// 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
public void onLongPress(MotionEvent e) {
Log.d("TAG", "[onLongPress]");
}
// 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d("TAG", "[onScroll]");
return true;
}
// 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发
// 注意和onDown()的区别,强调的是没有松开或者拖动的状态
public void onShowPress(MotionEvent e) {
Log.d("TAG", "[onShowPress]");
}
// 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
public boolean onSingleTapUp(MotionEvent e) {
Log.d("TAG", "[onSingleTapUp]");
return true;
}
}
3.main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/container">
<RelativeLayout android:layout_width="fill_parent"
android:id="@+id/container1" android:layout_height="fill_parent"
android:visibility="visible">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/one" />
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:background="@drawable/gray" android:id="@+id/container2"
android:visibility="gone" android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/three" />
</RelativeLayout>
</RelativeLayout>
4.程序所用到的anim中的几个xml文件内容
push_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=">
<translate android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="1000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="100" />
</set>
push_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="1000" />
<alpha android:fromAlpha="1.0" android:toAlpha="1.0"
android:duration="400" />
</set>
push_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=">
<translate android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="1000"/>
<alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" />
</set>
push_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=">
<translate android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="1000"/>
<alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" />
</set>
6.至于程序中所用到的图片,大家可以自己找几张,修改名称即可
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式