From c9ab275284d88af95cdd5397b5f83533eeef6b03 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sat, 3 Feb 2018 10:34:10 +0300 Subject: [PATCH] Added fling to context menu --- .../res/layout/map_context_menu_fragment.xml | 4 +- .../net/osmand/plus/LockableScrollView.java | 2 +- .../MapContextMenuFragment.java | 84 ++++++++++++++----- .../plus/mapcontextmenu/MenuController.java | 2 +- 4 files changed, 67 insertions(+), 25 deletions(-) diff --git a/OsmAnd/res/layout/map_context_menu_fragment.xml b/OsmAnd/res/layout/map_context_menu_fragment.xml index 60d0136612..0da95fecc0 100644 --- a/OsmAnd/res/layout/map_context_menu_fragment.xml +++ b/OsmAnd/res/layout/map_context_menu_fragment.xml @@ -91,7 +91,7 @@ android:layout_height= "wrap_content" android:layout_marginBottom="@dimen/context_menu_direction_margin" android:layout_marginTop="@dimen/context_menu_padding_margin_tiny" - android:gravity="center_vertical" + android:gravity="top" android:orientation="horizontal" android:paddingTop="3dp" android:paddingBottom="3dp" @@ -615,7 +615,7 @@ android:layout_height="@dimen/dashboard_map_toolbar" android:layout_marginLeft="4dp" android:layout_marginStart="4dp" - android:alpha="1"> + android:alpha="0"> maxVelocityY) { - maxVelocityY = velocityY; - } - } + float newEventY = newY - (dyMain - dy); + MotionEvent ev = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), event.getAction(), + event.getX(), newEventY, event.getMetaState()); + + initVelocityTrackerIfNotExists(); + velocityTracker.addMovement(ev); updateToolbar(); updateTopButton(); @@ -374,10 +385,28 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo moving = false; int currentY = getViewY(); - slidingUp = Math.abs(maxVelocityY) > 500 && (currentY - dyMain) < -50; - slidingDown = Math.abs(maxVelocityY) > 500 && (currentY - dyMain) > 50; + final VelocityTracker velocityTracker = this.velocityTracker; + velocityTracker.computeCurrentVelocity(1000, maximumVelocity); + int initialVelocity = (int) velocityTracker.getYVelocity(); - boolean skipScreenState = Math.abs(currentY - dyMain) > skipScreenStateLimit; + if ((Math.abs(initialVelocity) > minimumVelocity)) { + + scroller.abortAnimation(); + scroller.fling(0, currentY, 0, initialVelocity, 0, 0, + viewHeight - menuFullHeightMax, + minHalfY, + 0, 0); + currentY = scroller.getFinalY(); + scroller.abortAnimation(); + + slidingUp = initialVelocity < -2000; + slidingDown = initialVelocity > 2000; + } else { + slidingUp = false; + slidingDown = false; + } + + boolean skipScreenState = Math.abs(getViewY() - dyMain) > skipScreenStateLimit; changeMenuState(currentY, skipScreenState, slidingUp, slidingDown); } recycleVelocityTracker(); @@ -399,6 +428,13 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo } } + private void initVelocityTrackerIfNotExists() { + if (velocityTracker == null) { + velocityTracker = VelocityTracker.obtain(); + velocityTracker.clear(); + } + } + private void recycleVelocityTracker() { if (velocityTracker != null) { velocityTracker.recycle(); @@ -732,8 +768,7 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo int oldMenuState = menu.getCurrentMenuState(); if (!menu.isLandscapeLayout()) { - if (slidingDown && !skipScreenState && oldMenuState == MenuState.FULL_SCREEN - && currentY < (-menuTitleHeight + menuButtonsHeight)) { + if (slidingDown && oldMenuState == MenuState.FULL_SCREEN && getViewY() < getFullScreenTopPosY()) { slidingDown = false; } if (menuBottomViewHeight > 0 && slidingUp) { @@ -798,9 +833,9 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo private void applyPosY(final int currentY, final boolean needCloseMenu, boolean needMapAdjust, final int previousMenuState, final int newMenuState, int dZoom) { - final int posY = getPosY(currentY, needCloseMenu); - if (currentY != posY || dZoom != 0) { - if (posY < currentY) { + final int posY = getPosY(currentY, needCloseMenu, previousMenuState); + if (getViewY() != posY || dZoom != 0) { + if (posY < getViewY()) { updateMainViewLayout(posY); } @@ -1428,12 +1463,19 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo return viewHeight - menuTitleHeight; } + private int getFullScreenTopPosY() { + return -menuTitleHeight + menuButtonsHeight + bottomToolbarPosY; + } private int getPosY() { return getPosY(CURRENT_Y_UNDEFINED, false); } private int getPosY(final int currentY, boolean needCloseMenu) { + return getPosY(currentY, needCloseMenu, 0); + } + + private int getPosY(final int currentY, boolean needCloseMenu, int previousState) { if (needCloseMenu) { return screenHeight; } @@ -1462,12 +1504,12 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo if (menu.isLandscapeLayout()) { minPosY = topScreenPosY; } else { - minPosY = -menuTitleHeight + menuButtonsHeight + bottomToolbarPosY; + minPosY = getFullScreenTopPosY(); } if (maxPosY > minPosY) { maxPosY = minPosY; } - if (currentY > minPosY) { + if (currentY > minPosY || previousState != MenuState.FULL_SCREEN) { posY = minPosY; } else if (currentY < maxPosY) { posY = maxPosY; @@ -1478,7 +1520,7 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo if (menu.isLandscapeLayout()) { posY = topScreenPosY; } else { - posY = -menuTitleHeight + menuButtonsHeight + bottomToolbarPosY; + posY = getFullScreenTopPosY(); } } break; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java index df8894343f..af3f4ad44b 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java @@ -162,7 +162,7 @@ public abstract class MenuController extends BaseMenuController implements Colla @Override public void onCollapseExpand(boolean collapsed) { if (mapContextMenu != null) { - mapContextMenu.updateMenuUI(); + mapContextMenu.updateLayout(); } }