Gesture bezel with Android

I’m working on an application (on Galaxy Nexus), and I noticed that Google has implemented the Google Now application when you scroll from the bottom of the panel to the screen (the Chrome browser also did this for a while, not sure if it still does ) I searched around, but could not determine how they did it. Is there an easy way to handle gestures that start in a frame rather than on the screen?

I checked the developer pages for reference, but the only article I could find was here:

http://developer.android.com/design/patterns/gestures.html

Is there anywhere else where this information will be available?

[edit]

I tried very unsuccessfully to process gestures based on the Lain_B method, but I cannot get it to work. Here is the code I use to try to detect gestures, but logcat always outputs zero ...

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;

}
}

[Edit 2]

With the request Lain_B ...

(using Nexus 7) ... Starting from the maximum possible left point on the panel and scrolling straight to the middle (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

Scrolling from the center point of the screen, from the screen (to the right)

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
+6
source share
3 answers

I think MotionEvent.getEdgeFlags () is what you are looking for. You can then compare the return value with EDGE_LEFT , EDGE_RIGHT, etc., to see which edge has been touched.

if( event1.getEdgeFlags()&MotionEvent.EDGE_LEFT != 0 ){
    //code to handle swipe from left edge
}

or

if( e1.getX() < 5.0f ){
    //handle swipe from left edge
}
+9
source

MotionEvent.getEdgeFlags . . : https://groups.google.com/forum/?fromgroups=#!topic/android-developers/ZNjpzbkxeNM

/, , "x" "" .

- GestureDetector /. onScroll SimpleOnGestureListener , .

- ( GestureDetector, SimpleOnGestureListener):

 public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

        if (e1.getX() < SOME_DISTANCE_FROM_SCREEN) {
           //your code  
           return true;
        }
        return false;
 }
+7

, , / ( iOS), .

I created a Gesture Manager class that handles fling (for now) and compares gesture distance (start and end) with screen size.

Anyway, 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 :)

0
source

All Articles