diff --git a/README.md b/README.md index 031d2d3..a4da1f2 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,8 @@ You can customize some of the view effects programatically or using xml styleabl * Draggable view margin bottom applied when the view is minimized. * Enable or disable the horizontal alpha effect applied while the view is being horizontally dragged. * Enable or disable touch on minimized/maximized view to minimize/maximize. +* Set the minimized view to the left, center and right +* Change the drag limit, between 0.0 and 1.0 ```xml diff --git a/draggablepanel/build.gradle b/draggablepanel/build.gradle index 149fbde..0569058 100644 --- a/draggablepanel/build.gradle +++ b/draggablepanel/build.gradle @@ -30,7 +30,7 @@ android { } } -apply from: 'https://raw.github.com/jpardogo/gradle-mvn-push/master/gradle-mvn-push.gradle' +//apply from: 'https://raw.github.com/jpardogo/gradle-mvn-push/master/gradle-mvn-push.gradle' task checkstyle(type: Checkstyle) { diff --git a/draggablepanel/res/values/attrs.xml b/draggablepanel/res/values/attrs.xml index 7521a99..9d068e6 100644 --- a/draggablepanel/res/values/attrs.xml +++ b/draggablepanel/res/values/attrs.xml @@ -4,12 +4,16 @@ - + + + + + @@ -18,12 +22,24 @@ - + + + + + + + + + + + + + \ No newline at end of file diff --git a/draggablepanel/src/main/java/com/github/pedrovgs/DraggableListener.java b/draggablepanel/src/main/java/com/github/pedrovgs/DraggableListener.java index 1747659..4772fc3 100644 --- a/draggablepanel/src/main/java/com/github/pedrovgs/DraggableListener.java +++ b/draggablepanel/src/main/java/com/github/pedrovgs/DraggableListener.java @@ -42,4 +42,12 @@ public interface DraggableListener { * Called when the view is closed to the right. */ void onClosedToRight(); + + /** + * Called when the view is sliding. + * + * @param scroll return the scroll value, 0.0 is minimized and 1.0 is maximized. + */ + void onTopViewSlide(float scroll); + } diff --git a/draggablepanel/src/main/java/com/github/pedrovgs/DraggablePanel.java b/draggablepanel/src/main/java/com/github/pedrovgs/DraggablePanel.java index f1f0556..8836e67 100644 --- a/draggablepanel/src/main/java/com/github/pedrovgs/DraggablePanel.java +++ b/draggablepanel/src/main/java/com/github/pedrovgs/DraggablePanel.java @@ -21,6 +21,8 @@ import android.support.v4.app.FragmentManager; import android.util.AttributeSet; import android.widget.FrameLayout; +import com.github.pedrovgs.transformer.Position; +import com.github.pedrovgs.transformer.Transformer; /** * Custom view created to handle DraggableView using fragments. With this custom view the client @@ -50,14 +52,19 @@ public class DraggablePanel extends FrameLayout { private Fragment topFragment; private Fragment bottomFragment; private int topFragmentHeight; - private int topFragmentMarginRight; + private int topFragmentMargin; + private int topFragmentMarginTop; private int topFragmentMarginBottom; + private int topFragmentMarginLeft; + private int topFragmentMarginRight; private float xScaleFactor; private float yScaleFactor; private boolean enableHorizontalAlphaEffect; private boolean enableClickToMaximize; private boolean enableClickToMinimize; private boolean enableTouchListener; + private float dragLimit; + private int dragViewPosition; public DraggablePanel(Context context) { super(context); @@ -145,6 +152,24 @@ public void setClickToMinimizeEnabled(boolean enableClickToMinimize) { this.enableClickToMinimize = enableClickToMinimize; } + /** + * Enable or disable touch listener on drag view + * + * @param enableTouchListener to enable or disable the touch. + */ + public void setEnableTouchListener(boolean enableTouchListener) { + this.enableTouchListener = enableTouchListener; + } + + /** + * Configure the drag view limit. + * + * @param dragLimit in pixels + */ + public void setDragLimit(float dragLimit) { + this.dragLimit = dragLimit; + } + /** * * Slide the view based on scroll of the nav drawer. @@ -161,6 +186,21 @@ public void slideHorizontally(float slideOffset, float drawerPosition, int width draggableView.slideHorizontally(slideOffset, drawerPosition, width); } + /** + * Configure the position of the drag view then this is minimized, can be left, center or right. + */ + public void setDragViewPosition(int dragViewPosition) { + this.dragViewPosition = dragViewPosition; + } + + /** + * Configure the horizontal scale factor applied when the top fragment is dragged to the bottom + * of the custom view. + */ + public void setTopFragmentHeight(int topFragmentHeight) { + this.topFragmentHeight = topFragmentHeight; + } + /** * Configure the horizontal scale factor applied when the top fragment is dragged to the bottom * of the custom view. @@ -178,12 +218,21 @@ public void setYScaleFactor(float yScaleFactor) { } /** - * Configure the top Fragment margin right applied when the view has been minimized. + * Configure all margins of the dragView and are applied when the dragView is minimized. * - * @param topFragmentMarginRight in pixels. + * @param topFragmentMargin in pixels. */ - public void setTopFragmentMarginRight(int topFragmentMarginRight) { - this.topFragmentMarginRight = topFragmentMarginRight; + public void setTopFragmentMargin(int topFragmentMargin) { + this.topFragmentMargin = topFragmentMargin; + } + + /** + * Configure the top Fragment margin top applied when the view has been minimized. + * + * @param topFragmentMarginTop in pixels. + */ + public void setTopFragmentMarginTop(int topFragmentMarginTop) { + this.topFragmentMarginTop = topFragmentMarginTop; } /** @@ -195,6 +244,24 @@ public void setTopFragmentMarginBottom(int topFragmentMarginBottom) { this.topFragmentMarginBottom = topFragmentMarginBottom; } + /** + * Configure the top Fragment margin left applied when the view has been minimized. + * + * @param topFragmentMarginLeft in pixels. + */ + public void setTopFragmentMarginLeft(int topFragmentMarginLeft) { + this.topFragmentMarginLeft = topFragmentMarginLeft; + } + + /** + * Configure the top Fragment margin right applied when the view has been minimized. + * + * @param topFragmentMarginRight in pixels. + */ + public void setTopFragmentMarginRight(int topFragmentMarginRight) { + this.topFragmentMarginRight = topFragmentMarginRight; + } + /** * Configure the DraggableListener that is going to be invoked when the view be minimized, * maximized, closed to the left or right. @@ -252,7 +319,6 @@ public void minimize() { public void initializeView() { checkFragmentConsistency(); checkSupportFragmentManagerConsistency(); - inflate(getContext(), R.layout.draggable_panel, this); draggableView = (DraggableView) findViewById(R.id.draggable_view); draggableView.setTopViewHeight(topFragmentHeight); @@ -260,14 +326,19 @@ public void initializeView() { draggableView.attachTopFragment(topFragment); draggableView.setXTopViewScaleFactor(xScaleFactor); draggableView.setYTopViewScaleFactor(yScaleFactor); - draggableView.setTopViewMarginRight(topFragmentMarginRight); + draggableView.setTopViewMargin(topFragmentMargin); + draggableView.setTopViewMarginTop(topFragmentMarginTop); + draggableView.setTopViewMarginLeft(topFragmentMarginLeft); draggableView.setTopViewMarginBottom(topFragmentMarginBottom); + draggableView.setTopViewMarginRight(topFragmentMarginRight); draggableView.attachBottomFragment(bottomFragment); draggableView.setDraggableListener(draggableListener); draggableView.setHorizontalAlphaEffectEnabled(enableHorizontalAlphaEffect); draggableView.setClickToMaximizeEnabled(enableClickToMaximize); draggableView.setClickToMinimizeEnabled(enableClickToMinimize); draggableView.setTouchEnabled(enableTouchListener); + draggableView.setDragLimit(dragLimit); + draggableView.setDragViewPosition(dragViewPosition); } /** @@ -313,31 +384,35 @@ public boolean isClosedAtLeft() { */ private void initializeAttrs(AttributeSet attrs) { TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.draggable_panel); - this.topFragmentHeight = - attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_height, - DEFAULT_TOP_FRAGMENT_HEIGHT); - this.xScaleFactor = - attributes.getFloat(R.styleable.draggable_panel_x_scale_factor, DEFAULT_SCALE_FACTOR); - this.yScaleFactor = - attributes.getFloat(R.styleable.draggable_panel_y_scale_factor, DEFAULT_SCALE_FACTOR); - this.topFragmentMarginRight = - attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_margin_right, - DEFAULT_TOP_FRAGMENT_MARGIN); - this.topFragmentMarginBottom = - attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_margin_bottom, - DEFAULT_TOP_FRAGMENT_MARGIN); - this.enableHorizontalAlphaEffect = - attributes.getBoolean(R.styleable.draggable_panel_enable_horizontal_alpha_effect, - DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT); - this.enableClickToMaximize = - attributes.getBoolean(R.styleable.draggable_panel_enable_click_to_maximize_panel, - DEFAULT_ENABLE_CLICK_TO_MAXIMIZE); - this.enableClickToMinimize = - attributes.getBoolean(R.styleable.draggable_panel_enable_click_to_minimize_panel, - DEFAULT_ENABLE_CLICK_TO_MINIMIZE); - this.enableTouchListener = - attributes.getBoolean(R.styleable.draggable_panel_enable_touch_listener_panel, - DEFAULT_ENABLE_TOUCH_LISTENER); + setTopFragmentHeight(attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_height, + DEFAULT_TOP_FRAGMENT_HEIGHT)); + setXScaleFactor(attributes.getFloat(R.styleable.draggable_panel_x_scale_factor, + DEFAULT_SCALE_FACTOR)); + setYScaleFactor(attributes.getFloat(R.styleable.draggable_panel_y_scale_factor, + DEFAULT_SCALE_FACTOR)); + setTopFragmentMargin(attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_margin, + DEFAULT_TOP_FRAGMENT_MARGIN)); + setTopFragmentMarginTop(attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_margin_top, + DEFAULT_TOP_FRAGMENT_MARGIN)); + setTopFragmentMarginBottom(attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_margin_bottom, + DEFAULT_TOP_FRAGMENT_MARGIN)); + setTopFragmentMarginLeft(attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_margin_left, + DEFAULT_TOP_FRAGMENT_MARGIN)); + setTopFragmentMarginRight(attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_margin_right, + DEFAULT_TOP_FRAGMENT_MARGIN)); + setEnableHorizontalAlphaEffect(attributes.getBoolean(R.styleable.draggable_panel_enable_horizontal_alpha_effect, + DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT)); + setClickToMaximizeEnabled(attributes.getBoolean(R.styleable.draggable_panel_enable_click_to_maximize_panel, + DEFAULT_ENABLE_CLICK_TO_MAXIMIZE)); + setClickToMinimizeEnabled(attributes.getBoolean(R.styleable.draggable_panel_enable_click_to_minimize_panel, + DEFAULT_ENABLE_CLICK_TO_MINIMIZE)); + setEnableTouchListener(attributes.getBoolean(R.styleable.draggable_panel_enable_touch_listener_panel, + DEFAULT_ENABLE_TOUCH_LISTENER)); + setDragLimit(attributes.getFloat(R.styleable.draggable_panel_drag_limit_panel, + Transformer.DEFAULT_DRAG_LIMIT)); + setDragViewPosition(getContext().obtainStyledAttributes(attrs, R.styleable.top_view_position) + .getInt(R.styleable.top_view_position_position, + Position.RIGHT)); attributes.recycle(); } diff --git a/draggablepanel/src/main/java/com/github/pedrovgs/DraggableView.java b/draggablepanel/src/main/java/com/github/pedrovgs/DraggableView.java index 79fbbda..4f426df 100644 --- a/draggablepanel/src/main/java/com/github/pedrovgs/DraggableView.java +++ b/draggablepanel/src/main/java/com/github/pedrovgs/DraggableView.java @@ -27,6 +27,7 @@ import android.view.MotionEvent; import android.view.View; import android.widget.RelativeLayout; +import com.github.pedrovgs.transformer.Position; import com.github.pedrovgs.transformer.Transformer; import com.github.pedrovgs.transformer.TransformerFactory; import com.nineoldandroids.view.ViewHelper; @@ -41,13 +42,14 @@ public class DraggableView extends RelativeLayout { private static final int DEFAULT_SCALE_FACTOR = 2; private static final int DEFAULT_TOP_VIEW_MARGIN = 30; private static final int DEFAULT_TOP_VIEW_HEIGHT = -1; + private static final int MAX_ALPHA = 1; + private static final int MIN_ALPHA = 0; private static final float SLIDE_TOP = 0f; private static final float SLIDE_BOTTOM = 1f; private static final float MIN_SLIDE_OFFSET = 0.1f; private static final boolean DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT = true; private static final boolean DEFAULT_ENABLE_CLICK_TO_MAXIMIZE = false; private static final boolean DEFAULT_ENABLE_CLICK_TO_MINIMIZE = false; - private static final boolean DEFAULT_ENABLE_TOUCH_LISTENER = true; private static final int MIN_SLIDING_DISTANCE_ON_CLICK = 10; private static final int ONE_HUNDRED = 100; private static final float SENSITIVITY = 1f; @@ -58,9 +60,11 @@ public class DraggableView extends RelativeLayout { private View dragView; private View secondView; private TypedArray attributes; + private TypedArray attributePosition; private FragmentManager fragmentManager; private ViewDragHelper viewDragHelper; + private DraggableViewCallback draggableViewCallback; private Transformer transformer; private boolean enableHorizontalAlphaEffect; @@ -175,20 +179,56 @@ public void setYTopViewScaleFactor(float yScaleFactor) { transformer.setYScaleFactor(yScaleFactor); } + /** + * Configure all margins of the dragView and are applied when the dragView is minimized. + * + * @param topViewMargin in pixels. + */ + public void setTopViewMargin(int topViewMargin) { + transformer.setMargins(topViewMargin); + } + + /** + * Configure the dragView margin left applied when the dragView is minimized. + * + * @param topViewMarginLeft in pixels. + */ + public void setTopViewMarginLeft(int topViewMarginLeft) { + transformer.setMarginLeft(topViewMarginLeft); + } + /** * Configure the dragged view margin right applied when the dragged view is minimized. * - * @param topFragmentMarginRight in pixels. + * @param topViewMarginRight in pixels. */ - public void setTopViewMarginRight(int topFragmentMarginRight) { - transformer.setMarginRight(topFragmentMarginRight); + public void setTopViewMarginRight(int topViewMarginRight) { + transformer.setMarginRight(topViewMarginRight); + } + + /** + * Configure the dragView margin top applied when the dragView is minimized. + * + * @param topViewMarginTop in pixels. + */ + public void setTopViewMarginTop(int topViewMarginTop) { + transformer.setMarginTop(topViewMarginTop); } /** * Configure the dragView margin bottom applied when the dragView is minimized. + * + * @param topViewMarginBottom in pixels. + */ + public void setTopViewMarginBottom(int topViewMarginBottom) { + transformer.setMarginBottom(topViewMarginBottom); + } + + /** + * Configure the position of the drag view then this is minimized, can be left, center or right. */ - public void setTopViewMarginBottom(int topFragmentMarginBottom) { - transformer.setMarginBottom(topFragmentMarginBottom); + public void setDragViewPosition(int dragViewPosition) { + transformer.setViewPosition(dragViewPosition); } /** @@ -200,6 +240,15 @@ public void setTopViewHeight(int topFragmentHeight) { transformer.setViewHeight(topFragmentHeight); } + /** + * Configure the drag view limit. + * + * @param dragLimit, between 0.0 and 1.0 + */ + public void setDragLimit(float dragLimit) { + transformer.setDragLimit(dragLimit); + } + /** * Configure the disabling of the alpha effect applied when the dragView is dragged horizontally. */ @@ -209,10 +258,11 @@ public void setHorizontalAlphaEffectEnabled(boolean enableHorizontalAlphaEffect) /** * Configure the DraggableListener notified when the view is minimized, maximized, closed to the - * right or closed to the left. + * right or closed to the left and the actual position on Y axis. */ public void setDraggableListener(DraggableListener listener) { this.listener = listener; + draggableViewCallback.setDraggableListener(listener); } /** @@ -254,7 +304,7 @@ public void minimize() { */ public void closeToRight() { if (viewDragHelper.smoothSlideViewTo(dragView, transformer.getOriginalWidth(), - getHeight() - transformer.getMinHeightPlusMargin())) { + getHeight() - transformer.getMinHeightPlusVerticalSides())) { ViewCompat.postInvalidateOnAnimation(this); notifyCloseToRightListener(); } @@ -265,7 +315,7 @@ public void closeToRight() { */ public void closeToLeft() { if (viewDragHelper.smoothSlideViewTo(dragView, -transformer.getOriginalWidth(), - getHeight() - transformer.getMinHeightPlusMargin())) { + getHeight() - transformer.getMinHeightPlusVerticalSides())) { ViewCompat.postInvalidateOnAnimation(this); notifyCloseToLeftListener(); } @@ -403,9 +453,9 @@ private MotionEvent cloneMotionEventWithAction(MotionEvent event, int action) { * Override method to configure the dragged view and secondView layout properly. */ @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { - if (isInEditMode()) + if (isInEditMode()) { super.onLayout(changed, left, top, right, bottom); - else if (isDragViewAtTop()) { + } else if (isDragViewAtTop()) { dragView.layout(left, top, right, transformer.getOriginalHeight()); secondView.layout(left, transformer.getOriginalHeight(), right, bottom); ViewHelper.setY(dragView, top); @@ -493,7 +543,7 @@ void changeDragViewScale() { void changeBackgroundAlpha() { Drawable background = getBackground(); if (background != null) { - int newAlpha = (int) (ONE_HUNDRED * (1 - getVerticalDragOffset())); + int newAlpha = (int) (ONE_HUNDRED * (MAX_ALPHA - getVerticalDragOffset())); background.setAlpha(newAlpha); } } @@ -502,7 +552,7 @@ void changeBackgroundAlpha() { * Modify the second view alpha based on dragged view vertical position. */ void changeSecondViewAlpha() { - ViewHelper.setAlpha(secondView, 1 - getVerticalDragOffset()); + ViewHelper.setAlpha(secondView, MAX_ALPHA - getVerticalDragOffset()); } /** @@ -511,9 +561,9 @@ void changeSecondViewAlpha() { */ void changeDragViewViewAlpha() { if (enableHorizontalAlphaEffect) { - float alpha = 1 - getHorizontalDragOffset(); - if (alpha == 0) { - alpha = 1; + float alpha = MAX_ALPHA - getHorizontalDragOffset(); + if (alpha == MIN_ALPHA) { + alpha = MAX_ALPHA; } ViewHelper.setAlpha(dragView, alpha); } @@ -523,8 +573,8 @@ void changeDragViewViewAlpha() { * Restore view alpha to 1 */ void restoreAlpha() { - if (enableHorizontalAlphaEffect && ViewHelper.getAlpha(dragView) < 1) { - ViewHelper.setAlpha(dragView, 1); + if (enableHorizontalAlphaEffect && ViewHelper.getAlpha(dragView) < MAX_ALPHA) { + ViewHelper.setAlpha(dragView, MAX_ALPHA); } } @@ -564,6 +614,15 @@ boolean isDragViewAtTop() { return transformer.isViewAtTop(); } + /** + * Check if dragged view is at the left of the custom view. + * + * @return true if dragged view left position is equals to custom view width. + */ + boolean isDragViewAtLeft() { + return transformer.isViewAtLeft(); + } + /** * Check if dragged view is at the right of the custom view. * @@ -617,7 +676,9 @@ private void addFragmentToView(final int viewId, final Fragment fragment) { * Initialize the viewDragHelper. */ private void initializeViewDragHelper() { - viewDragHelper = ViewDragHelper.create(this, SENSITIVITY, new DraggableViewCallback(this, dragView)); + draggableViewCallback = new DraggableViewCallback(this, dragView, listener, + (int) getVerticalDragRange()); + viewDragHelper = ViewDragHelper.create(this, SENSITIVITY, draggableViewCallback); } /** @@ -628,20 +689,27 @@ private void initializeTransformer(TypedArray attributes) { attributes.getBoolean(R.styleable.draggable_view_top_view_resize, DEFAULT_TOP_VIEW_RESIZE); TransformerFactory transformerFactory = new TransformerFactory(); transformer = transformerFactory.getTransformer(topViewResize, dragView, this); - transformer.setViewHeight(attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_height, + setTopViewHeight(attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_height, DEFAULT_TOP_VIEW_HEIGHT)); - transformer.setXScaleFactor( - attributes.getFloat(R.styleable.draggable_view_top_view_x_scale_factor, - DEFAULT_SCALE_FACTOR)); - transformer.setYScaleFactor( - attributes.getFloat(R.styleable.draggable_view_top_view_y_scale_factor, - DEFAULT_SCALE_FACTOR)); - transformer.setMarginRight( - attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_margin_right, + setXTopViewScaleFactor(attributes.getFloat(R.styleable.draggable_view_top_view_x_scale_factor, + DEFAULT_SCALE_FACTOR)); + setYTopViewScaleFactor(attributes.getFloat(R.styleable.draggable_view_top_view_y_scale_factor, + DEFAULT_SCALE_FACTOR)); + setTopViewMargin(attributes.getDimensionPixelOffset(R.styleable.draggable_view_top_view_margin, + DEFAULT_TOP_VIEW_MARGIN)); + setTopViewMarginTop(attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_margin_top, + DEFAULT_TOP_VIEW_MARGIN)); + setTopViewMarginBottom(attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_margin_bottom, DEFAULT_TOP_VIEW_MARGIN)); - transformer.setMarginBottom( - attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_margin_bottom, + setTopViewMarginLeft(attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_margin_left, + DEFAULT_TOP_VIEW_MARGIN)); + setTopViewMarginRight(attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_margin_right, DEFAULT_TOP_VIEW_MARGIN)); + setDragLimit(attributes.getFloat(R.styleable.draggable_view_drag_limit_view, + Transformer.DEFAULT_DRAG_LIMIT)); + setDragViewPosition(attributePosition.getInt(R.styleable.top_view_position_position, + Position.RIGHT)); + attributes.recycle(); } /** @@ -651,15 +719,14 @@ private void initializeTransformer(TypedArray attributes) { */ private void initializeAttributes(AttributeSet attrs) { TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.draggable_view); - this.enableHorizontalAlphaEffect = - attributes.getBoolean(R.styleable.draggable_view_enable_minimized_horizontal_alpha_effect, - DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT); - this.enableClickToMaximize = - attributes.getBoolean(R.styleable.draggable_view_enable_click_to_maximize_view, - DEFAULT_ENABLE_CLICK_TO_MAXIMIZE); - this.enableClickToMinimize = - attributes.getBoolean(R.styleable.draggable_view_enable_click_to_minimize_view, - DEFAULT_ENABLE_CLICK_TO_MINIMIZE); + setHorizontalAlphaEffectEnabled(attributes.getBoolean( + R.styleable.draggable_view_enable_minimized_horizontal_alpha_effect, + DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT)); + setClickToMaximizeEnabled(attributes.getBoolean(R.styleable.draggable_view_enable_click_to_maximize_view, + DEFAULT_ENABLE_CLICK_TO_MAXIMIZE)); + setClickToMinimizeEnabled(attributes.getBoolean(R.styleable.draggable_view_enable_click_to_minimize_view, + DEFAULT_ENABLE_CLICK_TO_MINIMIZE)); + this.attributePosition = getContext().obtainStyledAttributes(attrs, R.styleable.top_view_position); this.attributes = attributes; } @@ -672,7 +739,7 @@ private void initializeAttributes(AttributeSet attrs) { */ private boolean smoothSlideTo(float slideOffset) { final int topBound = getPaddingTop(); - int x = (int) (slideOffset * (getWidth() - transformer.getMinWidthPlusMarginRight())); + int x = (int) (slideOffset * (getWidth() - transformer.getMinWidthPlusMarginHorizontalSides())); int y = (int) (topBound + slideOffset * getVerticalDragRange()); if (viewDragHelper.smoothSlideViewTo(dragView, x, y)) { ViewCompat.postInvalidateOnAnimation(this); @@ -681,6 +748,13 @@ private boolean smoothSlideTo(float slideOffset) { return false; } + /** + * @return configured dragged view margin left configured. + */ + private int getDragViewMarginLeft() { + return transformer.getMarginLeft(); + } + /** * @return configured dragged view margin right configured. */ @@ -688,6 +762,13 @@ private int getDragViewMarginRight() { return transformer.getMarginRight(); } + /** + * @return configured dragged view margin top. + */ + private int getDragViewMarginTop() { + return transformer.getMarginTop(); + } + /** * @return configured dragged view margin bottom. */ @@ -719,7 +800,7 @@ private float getVerticalDragOffset() { * @return the difference between the custom view height and the dragged view height. */ private float getVerticalDragRange() { - return getHeight() - transformer.getMinHeightPlusMargin(); + return getHeight() - transformer.getMinHeightPlusVerticalSides(); } /** @@ -759,6 +840,6 @@ private void notifyCloseToLeftListener() { } public int getDraggedViewHeightPlusMarginTop() { - return transformer.getMinHeightPlusMargin(); + return transformer.getMinHeightPlusVerticalSides(); } } diff --git a/draggablepanel/src/main/java/com/github/pedrovgs/DraggableViewCallback.java b/draggablepanel/src/main/java/com/github/pedrovgs/DraggableViewCallback.java index 08016ba..ba93cc6 100644 --- a/draggablepanel/src/main/java/com/github/pedrovgs/DraggableViewCallback.java +++ b/draggablepanel/src/main/java/com/github/pedrovgs/DraggableViewCallback.java @@ -31,6 +31,9 @@ class DraggableViewCallback extends ViewDragHelper.Callback { private static final float X_MIN_VELOCITY = 1500; private static final float Y_MIN_VELOCITY = 1000; + private int verticalDragRange = 0; + + private DraggableListener draggableListener; private DraggableView draggableView; private View draggedView; @@ -39,9 +42,20 @@ class DraggableViewCallback extends ViewDragHelper.Callback { * * @param draggableView instance used to apply some animations or visual effects. */ - public DraggableViewCallback(DraggableView draggableView, View draggedView) { + public DraggableViewCallback(DraggableView draggableView, View draggedView, + DraggableListener draggableListener, int height) { this.draggableView = draggableView; this.draggedView = draggedView; + this.draggableListener = draggableListener; + this.verticalDragRange = height; + } + + public void setDraggableListener(DraggableListener draggableListener) { + this.draggableListener = draggableListener; + } + + private int getVerticalDragRange() { + return verticalDragRange; } /** @@ -64,6 +78,16 @@ public DraggableViewCallback(DraggableView draggableView, View draggedView) { draggableView.changeSecondViewPosition(); draggableView.changeBackgroundAlpha(); } + + if (draggableListener != null) { + float fractionScreen = (float) Math.abs(top) / (float) verticalDragRange; + if (fractionScreen >= 1) { + fractionScreen = 1; + } + if (fractionScreen >= 0.0) { + draggableListener.onTopViewSlide(fractionScreen); + } + } } /** @@ -77,7 +101,6 @@ public DraggableViewCallback(DraggableView draggableView, View draggedView) { */ @Override public void onViewReleased(View releasedChild, float xVel, float yVel) { super.onViewReleased(releasedChild, xVel, yVel); - if (draggableView.isDragViewAtBottom() && !draggableView.isDragViewAtRight()) { triggerOnReleaseActionsWhileHorizontalDrag(xVel); } else { diff --git a/draggablepanel/src/main/java/com/github/pedrovgs/transformer/Position.java b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/Position.java new file mode 100644 index 0000000..f35f02e --- /dev/null +++ b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/Position.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2014 Pedro Vicente Gómez Sánchez. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.pedrovgs.transformer; + +/** + * This class represent the position of the top view + * + * @author Pedro Paulo de Amorim + */ +public class Position { + public static final int LEFT = 0; + public static final int CENTER = 1; + public static final int RIGHT = 2; +} diff --git a/draggablepanel/src/main/java/com/github/pedrovgs/transformer/ResizeTransformer.java b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/ResizeTransformer.java index 6af7bc3..5b5fbf3 100644 --- a/draggablepanel/src/main/java/com/github/pedrovgs/transformer/ResizeTransformer.java +++ b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/ResizeTransformer.java @@ -53,14 +53,38 @@ class ResizeTransformer extends Transformer { */ @Override public void updatePosition(float verticalDragOffset) { - int right = getViewRightPosition(verticalDragOffset); - int left = right - layoutParams.width; + + int right; + int left; int top = getView().getTop(); int bottom = top + layoutParams.height; + switch (getViewPosition()) { + case Position.LEFT: + left = getViewLeftPosition(verticalDragOffset); + right = layoutParams.width; + break; + case Position.CENTER: + right = layoutParams.width / 2; + left = right; + break; + case Position.RIGHT: + default: + right = getViewRightPosition(verticalDragOffset); + left = right - layoutParams.width; + break; + } + getView().layout(left, top, right, bottom); } + /** + * @return true if the left position of the view plus the right left is equals to the parent + * width. + */ + @Override public boolean isViewAtLeft() { + return getView().getLeft() + getMarginLeft() == getParentView().getWidth(); + } /** * @return true if the right position of the view plus the right margin is equals to the parent @@ -97,15 +121,15 @@ public void updatePosition(float verticalDragOffset) { /** * Uses the Y scale factor to calculate the min possible height. */ - @Override public int getMinHeightPlusMargin() { - return (int) (getOriginalHeight() * (1 - 1 / getYScaleFactor()) + getMarginBottom()); + @Override public int getMinHeightPlusVerticalSides() { + return (int) (getOriginalHeight() * (1 - 1 / getYScaleFactor()) + getMarginTop() + getMarginBottom()); } /** * Uses the X scale factor to calculate the min possible width. */ - @Override public int getMinWidthPlusMarginRight() { - return (int) (getOriginalWidth() * (1 - 1 / getXScaleFactor()) + getMarginRight()); + @Override public int getMinWidthPlusMarginHorizontalSides() { + return (int) (getOriginalWidth() * (1 - 1 / getXScaleFactor()) + getMarginLeft() + getMarginRight()); } /** @@ -117,4 +141,8 @@ private int getViewRightPosition(float verticalDragOffset) { return (int) ((getOriginalWidth()) - getMarginRight() * verticalDragOffset); } + private int getViewLeftPosition(float verticalDragOffset) { + return (int) ((getOriginalWidth()) - getMarginLeft() * verticalDragOffset); + } + } diff --git a/draggablepanel/src/main/java/com/github/pedrovgs/transformer/ScaleTransformer.java b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/ScaleTransformer.java index e49a13e..1a7dd85 100644 --- a/draggablepanel/src/main/java/com/github/pedrovgs/transformer/ScaleTransformer.java +++ b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/ScaleTransformer.java @@ -47,8 +47,15 @@ class ScaleTransformer extends Transformer { * @param verticalDragOffset used to calculate the new position. */ @Override public void updatePosition(float verticalDragOffset) { - ViewHelper.setPivotX(getView(), getView().getWidth() - getMarginRight()); - ViewHelper.setPivotY(getView(), getView().getHeight() - getMarginBottom()); + ViewHelper.setPivotX(getView(), generateHorizontalPivot()); + ViewHelper.setPivotY(getView(), generateVerticalPivot()); + } + + /** + * @return true if the left corner of the view matches with the parent view width. + */ + @Override public boolean isViewAtLeft() { + return getView().getLeft() == getParentView().getWidth(); } /** @@ -84,15 +91,31 @@ class ScaleTransformer extends Transformer { /** * @return min view height taking into account the configured margin. */ - @Override public int getMinHeightPlusMargin() { + @Override public int getMinHeightPlusVerticalSides() { return getView().getHeight(); } /** * @return min view width. */ - @Override public int getMinWidthPlusMarginRight() { + @Override public int getMinWidthPlusMarginHorizontalSides() { return getOriginalWidth(); } + private float generateHorizontalPivot() { + switch (getViewPosition()) { + case Position.LEFT: + return getMarginLeft(); + case Position.CENTER: + return getView().getWidth() / 2; + case Position.RIGHT: + default: + return getView().getWidth() - getMarginRight(); + } + } + + private float generateVerticalPivot() { + return getView().getHeight() - getMarginBottom(); + } + } diff --git a/draggablepanel/src/main/java/com/github/pedrovgs/transformer/Transformer.java b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/Transformer.java index 93a248c..639325e 100644 --- a/draggablepanel/src/main/java/com/github/pedrovgs/transformer/Transformer.java +++ b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/Transformer.java @@ -34,10 +34,14 @@ */ public abstract class Transformer { + public static final float DEFAULT_DRAG_LIMIT = 0.5f; + private final View view; private final View parent; + private int marginLeft; private int marginRight; + private int marginTop; private int marginBottom; private float xScaleFactor; @@ -46,11 +50,22 @@ public abstract class Transformer { private int originalHeight; private int originalWidth; + private int position; + private float dragLimit; + public Transformer(View view, View parent) { this.view = view; this.parent = parent; } + public int getViewPosition() { + return position; + } + + public void setViewPosition(int position) { + this.position = position; + } + public float getXScaleFactor() { return xScaleFactor; } @@ -67,6 +82,22 @@ public void setYScaleFactor(float yScaleFactor) { this.yScaleFactor = yScaleFactor; } + public void setMargins(int margins) { + margins = Math.round(margins); + this.marginLeft = margins; + this.marginRight = margins; + this.marginTop = margins; + this.marginBottom = margins; + } + + public int getMarginLeft() { + return marginLeft; + } + + public void setMarginLeft(int marginLeft) { + this.marginLeft = Math.round(marginLeft); + } + public int getMarginRight() { return marginRight; } @@ -75,6 +106,14 @@ public void setMarginRight(int marginRight) { this.marginRight = Math.round(marginRight); } + public int getMarginTop() { + return marginTop; + } + + public void setMarginTop(int marginTop) { + this.marginTop = Math.round(marginTop); + } + public int getMarginBottom() { return marginBottom; } @@ -83,6 +122,14 @@ public void setMarginBottom(int marginBottom) { this.marginBottom = Math.round(marginBottom); } + public float getDragLimit() { + return dragLimit; + } + + public void setDragLimit(float dragLimit) { + this.dragLimit = dragLimit; + } + /** * Change view height using the LayoutParams of the view. * @@ -136,10 +183,12 @@ public boolean isViewAtTop() { public boolean isAboveTheMiddle() { int parentHeight = parent.getHeight(); - float viewYPosition = ViewHelper.getY(view) + (view.getHeight() * 0.5f); - return viewYPosition < (parentHeight * 0.5); + float viewYPosition = ViewHelper.getY(view) + (view.getHeight() * getDragLimit()); + return viewYPosition < (parentHeight * getDragLimit()); } + public abstract boolean isViewAtLeft(); + public abstract boolean isViewAtRight(); public abstract boolean isViewAtBottom(); @@ -149,12 +198,12 @@ public boolean isAboveTheMiddle() { public abstract boolean isNextToLeftBound(); /** - * @return min possible height, after apply the transformation, plus the margin right. + * @return min possible height, after apply the transformation, plus the margin right and left. */ - public abstract int getMinHeightPlusMargin(); + public abstract int getMinHeightPlusVerticalSides(); /** - * @return min possible width, after apply the transformation. + * @return min possible width, after apply the transformation, plus the margin top and bottom. */ - public abstract int getMinWidthPlusMarginRight(); + public abstract int getMinWidthPlusMarginHorizontalSides(); } diff --git a/draggablepanel/src/main/java/com/github/pedrovgs/transformer/TransformerFactory.java b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/TransformerFactory.java index 149714c..5375223 100644 --- a/draggablepanel/src/main/java/com/github/pedrovgs/transformer/TransformerFactory.java +++ b/draggablepanel/src/main/java/com/github/pedrovgs/transformer/TransformerFactory.java @@ -26,7 +26,7 @@ public class TransformerFactory { public Transformer getTransformer(final boolean resize, final View view, final View parent) { - Transformer transformer = null; + Transformer transformer; if (resize) { transformer = new ResizeTransformer(view, parent); } else { diff --git a/sample/build.gradle b/sample/build.gradle index 047b963..293dbc2 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -43,7 +43,6 @@ android { } } - task checkstyle(type: Checkstyle) { configFile file('../config/checkstyle/checkstyle.xml') source 'src/main/java' diff --git a/sample/res/layout/activity_tv_shows_sample.xml b/sample/res/layout/activity_tv_shows_sample.xml index 10c4855..13c3f66 100644 --- a/sample/res/layout/activity_tv_shows_sample.xml +++ b/sample/res/layout/activity_tv_shows_sample.xml @@ -26,8 +26,10 @@ draggable_view:bottom_view_id="@+id/lv_episodes" draggable_view:top_view_x_scale_factor="@dimen/x_scale_factor" draggable_view:top_view_y_scale_factor="@dimen/y_scale_factor" - draggable_view:top_view_margin_right="@dimen/top_fragment_margin" + draggable_view:top_view_margin_top="@dimen/top_fragment_margin" draggable_view:top_view_margin_bottom="@dimen/top_fragment_margin" + draggable_view:top_view_margin_left="@dimen/top_fragment_margin" + draggable_view:top_view_margin_right="@dimen/top_fragment_margin" android:background="@color/black"> diff --git a/sample/res/layout/activity_video_sample.xml b/sample/res/layout/activity_video_sample.xml index 2ce622a..4e45db8 100644 --- a/sample/res/layout/activity_video_sample.xml +++ b/sample/res/layout/activity_video_sample.xml @@ -25,10 +25,11 @@ draggable_view:top_view_x_scale_factor="@dimen/x_scale_factor" draggable_view:top_view_y_scale_factor="@dimen/y_scale_factor" draggable_view:top_view_height="@dimen/top_fragment_height" - draggable_view:top_view_margin_right="@dimen/top_fragment_margin" + draggable_view:top_view_margin_top="@dimen/top_fragment_margin" draggable_view:top_view_margin_bottom="@dimen/top_fragment_margin" + draggable_view:top_view_margin_left="@dimen/top_fragment_margin" + draggable_view:top_view_margin_right="@dimen/top_fragment_margin" draggable_view:enable_minimized_horizontal_alpha_effect="false" - draggable_view:top_view_resize="true" android:background="@color/black"> diff --git a/sample/res/layout/activity_youtube_sample.xml b/sample/res/layout/activity_youtube_sample.xml index c278f12..966cc84 100644 --- a/sample/res/layout/activity_youtube_sample.xml +++ b/sample/res/layout/activity_youtube_sample.xml @@ -23,8 +23,10 @@ draggable_panel:x_scale_factor="@dimen/x_scale_factor" draggable_panel:y_scale_factor="@dimen/y_scale_factor" draggable_panel:top_fragment_height="@dimen/top_fragment_height" - draggable_panel:top_fragment_margin_right="@dimen/top_fragment_margin" + draggable_panel:top_fragment_margin_top="@dimen/top_fragment_margin" draggable_panel:top_fragment_margin_bottom="@dimen/top_fragment_margin" + draggable_panel:top_fragment_margin_left="@dimen/top_fragment_margin" + draggable_panel:top_fragment_margin_right="@dimen/top_fragment_margin" draggable_panel:enable_horizontal_alpha_effect="false"/> \ No newline at end of file diff --git a/sample/src/main/java/com/github/pedrovgs/sample/activity/TvShowsActivity.java b/sample/src/main/java/com/github/pedrovgs/sample/activity/TvShowsActivity.java index ebf2dc5..220db1b 100644 --- a/sample/src/main/java/com/github/pedrovgs/sample/activity/TvShowsActivity.java +++ b/sample/src/main/java/com/github/pedrovgs/sample/activity/TvShowsActivity.java @@ -70,9 +70,10 @@ public class TvShowsActivity extends DIFragmentActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tv_shows_sample); ButterKnife.inject(this); + hookListeners(); initializeDraggableView(); initializeGridView(); - hookListeners(); + } /** @@ -139,6 +140,10 @@ private void hookListeners() { @Override public void onClosedToRight() { resetActionBarTitle(); } + + @Override public void onTopViewSlide(float scroll) { + + } }); } diff --git a/sample/src/main/java/com/github/pedrovgs/sample/activity/VideoSampleActivity.java b/sample/src/main/java/com/github/pedrovgs/sample/activity/VideoSampleActivity.java index 83e2dbf..d9cfb08 100644 --- a/sample/src/main/java/com/github/pedrovgs/sample/activity/VideoSampleActivity.java +++ b/sample/src/main/java/com/github/pedrovgs/sample/activity/VideoSampleActivity.java @@ -58,9 +58,9 @@ public class VideoSampleActivity extends FragmentActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_sample); ButterKnife.inject(this); + hookDraggableViewListener(); initializeVideoView(); initializePoster(); - hookDraggableViewListener(); } /** @@ -100,6 +100,10 @@ private void hookDraggableViewListener() { @Override public void onClosedToRight() { pauseVideo(); } + + @Override public void onTopViewSlide(float scroll) { + + } }); } diff --git a/sample/src/main/java/com/github/pedrovgs/sample/activity/YoutubeSampleActivity.java b/sample/src/main/java/com/github/pedrovgs/sample/activity/YoutubeSampleActivity.java index 756e20d..98e279b 100644 --- a/sample/src/main/java/com/github/pedrovgs/sample/activity/YoutubeSampleActivity.java +++ b/sample/src/main/java/com/github/pedrovgs/sample/activity/YoutubeSampleActivity.java @@ -61,8 +61,8 @@ public class YoutubeSampleActivity extends FragmentActivity { setContentView(R.layout.activity_youtube_sample); ButterKnife.inject(this); initializeYoutubeFragment(); - initializeDraggablePanel(); hookDraggablePanelListeners(); + initializeDraggablePanel(); } /** @@ -123,9 +123,7 @@ private void hookDraggablePanelListeners() { playVideo(); } - @Override public void onMinimized() { - //Empty - } + @Override public void onMinimized() { } @Override public void onClosedToLeft() { pauseVideo(); @@ -134,6 +132,8 @@ private void hookDraggablePanelListeners() { @Override public void onClosedToRight() { pauseVideo(); } + + @Override public void onTopViewSlide(float scroll) { } }); }