|
@@ -4,15 +4,17 @@ import android.annotation.SuppressLint;
|
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
|
import android.os.Handler;
|
|
import android.os.Handler;
|
|
|
import android.util.AttributeSet;
|
|
import android.util.AttributeSet;
|
|
|
|
|
+import android.util.Log;
|
|
|
import android.view.MotionEvent;
|
|
import android.view.MotionEvent;
|
|
|
import android.view.View;
|
|
import android.view.View;
|
|
|
import android.view.animation.Interpolator;
|
|
import android.view.animation.Interpolator;
|
|
|
import android.widget.LinearLayout;
|
|
import android.widget.LinearLayout;
|
|
|
|
|
+import android.widget.ScrollView;
|
|
|
import android.widget.Scroller;
|
|
import android.widget.Scroller;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Extension of LinearLayout to be used in the app. Contains the slider menu.
|
|
* Extension of LinearLayout to be used in the app. Contains the slider menu.
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @author Iñigo Valentin
|
|
* @author Iñigo Valentin
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
@@ -20,13 +22,13 @@ public class MainLayout extends LinearLayout {
|
|
|
|
|
|
|
|
//The width of the view
|
|
//The width of the view
|
|
|
private int mainLayoutWidth;
|
|
private int mainLayoutWidth;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
//The menu view
|
|
//The menu view
|
|
|
private View menu;
|
|
private View menu;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
//The content view
|
|
//The content view
|
|
|
private View content;
|
|
private View content;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
//The margin of the menu
|
|
//The margin of the menu
|
|
|
private static int menuRightMargin = 25;
|
|
private static int menuRightMargin = 25;
|
|
|
|
|
|
|
@@ -39,15 +41,17 @@ public class MainLayout extends LinearLayout {
|
|
|
|
|
|
|
|
private int contentXOffset;
|
|
private int contentXOffset;
|
|
|
private MenuState currentMenuState = MenuState.HIDDEN;
|
|
private MenuState currentMenuState = MenuState.HIDDEN;
|
|
|
- private final Scroller menuScroller = new Scroller(this.getContext(), new EaseInInterpolator());
|
|
|
|
|
- private final Runnable menuRunnable = new MenuRunnable();
|
|
|
|
|
- private final Handler menuHandler = new Handler();
|
|
|
|
|
|
|
+ private Scroller menuScroller = new Scroller(this.getContext(), new EaseInInterpolator());
|
|
|
|
|
+ private Runnable menuRunnable = new MenuRunnable();
|
|
|
|
|
+ private Handler menuHandler = new Handler();
|
|
|
private int prevX = 0;
|
|
private int prevX = 0;
|
|
|
private boolean isDragging = false;
|
|
private boolean isDragging = false;
|
|
|
|
|
+ private int lastDiffX = 0;
|
|
|
|
|
+ private int initialX = 0;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Constructor.
|
|
* Constructor.
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param context Context of the app.
|
|
* @param context Context of the app.
|
|
|
* @param attrs Atributes.
|
|
* @param attrs Atributes.
|
|
|
*/
|
|
*/
|
|
@@ -57,7 +61,7 @@ public class MainLayout extends LinearLayout {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Constructor.
|
|
* Constructor.
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param context Context of the app.
|
|
* @param context Context of the app.
|
|
|
*/
|
|
*/
|
|
|
public MainLayout(Context context) {
|
|
public MainLayout(Context context) {
|
|
@@ -71,7 +75,7 @@ public class MainLayout extends LinearLayout {
|
|
|
mainLayoutWidth = MeasureSpec.getSize(widthMeasureSpec);
|
|
mainLayoutWidth = MeasureSpec.getSize(widthMeasureSpec);
|
|
|
menuRightMargin = mainLayoutWidth * 25 / 100;
|
|
menuRightMargin = mainLayoutWidth * 25 / 100;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@SuppressLint("ClickableViewAccessibility") //I don't need it.
|
|
@SuppressLint("ClickableViewAccessibility") //I don't need it.
|
|
|
@Override
|
|
@Override
|
|
|
protected void onAttachedToWindow() {
|
|
protected void onAttachedToWindow() {
|
|
@@ -105,13 +109,13 @@ public class MainLayout extends LinearLayout {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Changes the menu state.
|
|
|
|
|
|
|
+ * Changes the menu state.
|
|
|
* Closes it if it's open. Opens it if it's closed.
|
|
* Closes it if it's open. Opens it if it's closed.
|
|
|
*/
|
|
*/
|
|
|
public void toggleMenu() {
|
|
public void toggleMenu() {
|
|
|
if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
|
|
if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
|
|
|
return;
|
|
return;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
switch (currentMenuState) {
|
|
switch (currentMenuState) {
|
|
|
case HIDDEN:
|
|
case HIDDEN:
|
|
|
currentMenuState = MenuState.SHOWING;
|
|
currentMenuState = MenuState.SHOWING;
|
|
@@ -128,33 +132,33 @@ public class MainLayout extends LinearLayout {
|
|
|
menuHandler.postDelayed(menuRunnable, GM.MENU_QUERY_INTERVAL);
|
|
menuHandler.postDelayed(menuRunnable, GM.MENU_QUERY_INTERVAL);
|
|
|
this.invalidate();
|
|
this.invalidate();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Handles the animation of the sliding menu.
|
|
* Handles the animation of the sliding menu.
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @author seavenois
|
|
* @author seavenois
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @see Runnable
|
|
* @see Runnable
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- private class MenuRunnable implements Runnable {
|
|
|
|
|
|
|
+ protected class MenuRunnable implements Runnable {
|
|
|
@Override
|
|
@Override
|
|
|
public void run() {
|
|
public void run() {
|
|
|
boolean isScrolling = menuScroller.computeScrollOffset();
|
|
boolean isScrolling = menuScroller.computeScrollOffset();
|
|
|
adjustContentPosition(isScrolling);
|
|
adjustContentPosition(isScrolling);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * Moves the content view when the menu is slided.
|
|
|
|
|
- *
|
|
|
|
|
|
|
+ * Moves the content view when the menu is slided.
|
|
|
|
|
+ *
|
|
|
* @param isScrolling Indicates if the menu is being slided.
|
|
* @param isScrolling Indicates if the menu is being slided.
|
|
|
*/
|
|
*/
|
|
|
private void adjustContentPosition(boolean isScrolling) {
|
|
private void adjustContentPosition(boolean isScrolling) {
|
|
|
int scrollerXOffset = menuScroller.getCurrX();
|
|
int scrollerXOffset = menuScroller.getCurrX();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
content.offsetLeftAndRight(scrollerXOffset - contentXOffset);
|
|
content.offsetLeftAndRight(scrollerXOffset - contentXOffset);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
contentXOffset = scrollerXOffset;
|
|
contentXOffset = scrollerXOffset;
|
|
|
this.invalidate();
|
|
this.invalidate();
|
|
|
if (isScrolling)
|
|
if (isScrolling)
|
|
@@ -162,9 +166,9 @@ public class MainLayout extends LinearLayout {
|
|
|
else
|
|
else
|
|
|
this.onMenuSlidingComplete();
|
|
this.onMenuSlidingComplete();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * Called when the menu has been slided completely.
|
|
|
|
|
|
|
+ * Called when the menu has been slided completely.
|
|
|
* Fixes the position.
|
|
* Fixes the position.
|
|
|
*/
|
|
*/
|
|
|
private void onMenuSlidingComplete() {
|
|
private void onMenuSlidingComplete() {
|
|
@@ -178,29 +182,29 @@ public class MainLayout extends LinearLayout {
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Defines the menu animation frame rate.
|
|
* Defines the menu animation frame rate.
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @author Iñigo Valentin
|
|
* @author Iñigo Valentin
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
- private class EaseInInterpolator implements Interpolator {
|
|
|
|
|
|
|
+ protected class EaseInInterpolator implements Interpolator {
|
|
|
@Override
|
|
@Override
|
|
|
public float getInterpolation(float t) {
|
|
public float getInterpolation(float t) {
|
|
|
return (float) Math.pow(t - 1, 5) + 1;
|
|
return (float) Math.pow(t - 1, 5) + 1;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Register touch events.
|
|
|
|
|
|
|
+ * Register touch events.
|
|
|
* Used to show and hide the menu.
|
|
* Used to show and hide the menu.
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @param event The MotionEvent triggering it.
|
|
* @param event The MotionEvent triggering it.
|
|
|
* @return True if the menu is fully opened or closed and a touch event is happening
|
|
* @return True if the menu is fully opened or closed and a touch event is happening
|
|
|
*/
|
|
*/
|
|
|
- private boolean onContentTouch(MotionEvent event) {
|
|
|
|
|
|
|
+ public boolean onContentTouch(MotionEvent event) {
|
|
|
if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
|
|
if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
|
|
|
return false;
|
|
return false;
|
|
|
int curX = (int) event.getRawX();
|
|
int curX = (int) event.getRawX();
|
|
@@ -209,9 +213,13 @@ public class MainLayout extends LinearLayout {
|
|
|
switch (event.getAction()) {
|
|
switch (event.getAction()) {
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
prevX = curX;
|
|
prevX = curX;
|
|
|
|
|
+ initialX = curX;
|
|
|
return true;
|
|
return true;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
|
+ //int width = this.getWidth();
|
|
|
|
|
+ //if (event.getX() < (float) width / 4){
|
|
|
|
|
+
|
|
|
if (!isDragging) {
|
|
if (!isDragging) {
|
|
|
|
|
|
|
|
isDragging = true;
|
|
isDragging = true;
|
|
@@ -229,15 +237,16 @@ public class MainLayout extends LinearLayout {
|
|
|
this.invalidate();
|
|
this.invalidate();
|
|
|
|
|
|
|
|
prevX = curX;
|
|
prevX = curX;
|
|
|
-
|
|
|
|
|
|
|
+ lastDiffX = diffX;
|
|
|
|
|
+ //}
|
|
|
return true;
|
|
return true;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
case MotionEvent.ACTION_UP:
|
|
case MotionEvent.ACTION_UP:
|
|
|
- if (currentMenuState == MenuState.HIDDEN) {
|
|
|
|
|
|
|
+ if (initialX != curX && currentMenuState == MenuState.HIDDEN) {
|
|
|
currentMenuState = MenuState.SHOWING;
|
|
currentMenuState = MenuState.SHOWING;
|
|
|
menuScroller.startScroll(contentXOffset, 0, menu.getLayoutParams().width - contentXOffset, 0, GM.MENU_SLIDING_DURATION);
|
|
menuScroller.startScroll(contentXOffset, 0, menu.getLayoutParams().width - contentXOffset, 0, GM.MENU_SLIDING_DURATION);
|
|
|
}
|
|
}
|
|
|
- else if (currentMenuState == MenuState.SHOWN) {
|
|
|
|
|
|
|
+ else if (initialX != curX && currentMenuState == MenuState.SHOWN) {
|
|
|
|
|
|
|
|
currentMenuState = MenuState.HIDING;
|
|
currentMenuState = MenuState.HIDING;
|
|
|
menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0, GM.MENU_SLIDING_DURATION);
|
|
menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0, GM.MENU_SLIDING_DURATION);
|
|
@@ -246,14 +255,15 @@ public class MainLayout extends LinearLayout {
|
|
|
this.invalidate();
|
|
this.invalidate();
|
|
|
isDragging = false;
|
|
isDragging = false;
|
|
|
prevX = 0;
|
|
prevX = 0;
|
|
|
|
|
+ lastDiffX = 0;
|
|
|
return true;
|
|
return true;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
default:
|
|
default:
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|