我正在开发一个应用程序(在 Galaxy Nexus 上),我注意到当您从挡板底部滑动到屏幕上时,Google 实现了“Google Now”应用程序(chrome 浏览器也这样做了一段时间,而不是确定它是否仍然存在)。我一直在环顾四周,但无法确定他们是如何做到这一点的。有没有一种简单的方法来处理从边框而不是屏幕开始的手势?
我检查了开发人员页面以供参考,但我能找到的唯一文章在这里:
http://developer.android.com/design/patterns/gestures.html
有没有其他地方可以获得这些信息?
[编辑]
我一直在尝试根据 Lain_B 的方法使边缘手势工作,但未能成功,但无法使其工作。这是我用来尝试检测手势的代码,但 logcat 总是输出零......
public class MainActivity extends Activity implements OnGestureListener {
private GestureDetector myGesture;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myGesture = new GestureDetector(getBaseContext(),
(OnGestureListener) this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("Flags Touch", "Flags: " + event.getEdgeFlags());
return myGesture.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
Log.e("Flags", "Flags: " + e.getEdgeFlags());
Log.e("Event", "onDown");
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Log.e("Event", "onLongPress");
// TODO Auto-generated method stub
}
@Override
public void onShowPress(MotionEvent e) {
Log.e("Flags", "Flags: " + e.getEdgeFlags());
Log.e("Event", "onShowPress");
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.e("Event", "onSingleTapUp");
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// Log.e("Event", "onScroll");
return false;
}
// these constants are used for onFling
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.e("Event", "onFling");
Log.e("Flags", "Flags: " + e1.getEdgeFlags());
if (e1.getEdgeFlags() == MotionEvent.EDGE_LEFT) {
// code to handle swipe from left edge
Log.e("!!!!!", "Edge fling!");
}
try {
// do not do anything if the swipe does not reach a certain length
// of distance
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) < SWIPE_THRESHOLD_VELOCITY) {
}
// left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) < SWIPE_THRESHOLD_VELOCITY) {
}
} catch (Exception e) {
// nothing
}
return false;
}
}
[编辑 2]
使用 Lain_B 的请求输出...
(使用 Nexus 7)... 从挡板上可能的最左侧点开始,然后向右滑动到中间(ish)。
08-16 16:44:13.674: I/Fling(16702): Flags: 0
08-16 16:44:13.674: I/Fling(16702): e1: 2.5 711.5152
08-16 16:44:13.674: I/Fling(16702): e2: 215.4591 717.08105
从屏幕中心点滑出屏幕(向右)
08-16 16:46:37.364: I/Fling(16702): Flags: 0
08-16 16:46:37.364: I/Fling(16702): e1: 392.5 758.1818
08-16 16:46:37.364: I/Fling(16702): e2: 783.4375 743.3334