我正在开发一个应用程序(在Galaxy Nexus上),我注意到当你从屏幕底部滑动到屏幕上时谷歌实施了“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
答案 0 :(得分:9)
我认为MotionEvent.getEdgeFlags()正是您所寻找的。然后,您可以将返回值与EDGE_LEFT,EDGE_RIGHT等进行比较,以查看触摸的边缘。
if( event1.getEdgeFlags()&MotionEvent.EDGE_LEFT != 0 ){
//code to handle swipe from left edge
}
或
if( e1.getX() < 5.0f ){
//handle swipe from left edge
}
答案 1 :(得分:7)
依赖于MotionEvent.getEdgeFlags是错误的。有关详细信息,请参阅此讨论:https://groups.google.com/forum/?fromgroups=#!topic/android-developers/ZNjpzbkxeNM
从概念上讲,要检测边缘滑动,您需要检测拖动/滚动事件,并确保向下事件的原始“x”坐标距边缘一定距离。
最简单的方法是使用GestureDetector检测拖动/滚动。幸运的是,SimpleOnGestureListener的onScroll方法拥有你需要的一切。
这样的事情(如果你使用的是配置了SimpleOnGestureListener的GestureDetector):
public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (e1.getX() < SOME_DISTANCE_FROM_SCREEN) {
//your code
return true;
}
return false;
}
答案 2 :(得分:1)
Well, the solution for me was rather different as I was trying to achieve edge swipe / border swipe feature (similar to iOS) but from right to left.
I created a Gesture Manager class that handles fling (for now) and compares the distance of the gesture(start and end) to the size of the screen.
Anyhow, here it is:
public class GestureManager {
private static final String TAG = GestureManager.class.getSimpleName();
private Context context;
private Presenter presenter;
private GestureDetector gesture;
public GestureManager(Context context, Presenter presenter) {
this.context = context;
this.presenter = presenter;
this.gesture = new GestureDetector(context,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int deviceWidth = Helper.getDeviceWith();
//Log.d(TAG, "onFling e1: "+e1.getX()+" e2: "+e2.getX()+" deviceWidth: "+deviceWidth * 0.80);
if (context instanceof DeckMainActivity){
if (!presenter.isSideBarVisible() && e1.getX() > deviceWidth * 0.98 && e2.getX() > deviceWidth * 0.80) {
presenter.showSideBar();
return true;
}
}
return false;
}
});
}
public GestureDetector getGesture(){
return gesture;
}
Hope it helps :)