Skip to content
This repository has been archived by the owner on Nov 22, 2017. It is now read-only.

fix minimised view position on orientation change #71

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
<!--<module name="InnerAssignment"/>-->
<!--module name="MagicNumber"/-->
<module name="MissingSwitchDefault"/>
<module name="RedundantThrows"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>

Expand All @@ -131,4 +130,4 @@
<!--module name="TodoComment"/-->
<module name="UpperEll"/>
</module>
</module>
</module>
6 changes: 4 additions & 2 deletions draggablepanel/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.pedrovgs">
package="com.github.pedrovgs"
>

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20"/>
android:targetSdkVersion="20"
/>

</manifest>
2 changes: 1 addition & 1 deletion draggablepanel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'checkstyle'

dependencies {
compile 'com.android.support:support-v4:20.0.0'
compile 'com.android.support:support-v4:20.0.0'
compile 'com.nineoldandroids:library:2.4.0'
}

Expand Down
9 changes: 6 additions & 3 deletions draggablepanel/res/layout/draggable_panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent"
draggable_view:bottom_view_id="@+id/second_view"
draggable_view:top_view_id="@+id/drag_view">
draggable_view:top_view_id="@+id/drag_view"
>

<FrameLayout
android:id="@+id/second_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/drag_view"/>
android:layout_below="@+id/drag_view"
/>

<FrameLayout
android:id="@+id/drag_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"/>
android:orientation="vertical"
/>

</com.github.pedrovgs.DraggableView>
60 changes: 40 additions & 20 deletions draggablepanel/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="draggable_panel">
<attr name="top_fragment_height" format="dimension"/>
<attr name="x_scale_factor" format="float"/>
<attr name="y_scale_factor" format="float"/>
<attr name="top_fragment_margin_right" format="dimension"/>
<attr name="top_fragment_margin_bottom" format="dimension"/>
<attr name="enable_horizontal_alpha_effect" format="boolean"/>
<attr name="enable_click_to_maximize_panel" format="boolean"/>
<attr name="enable_click_to_minimize_panel" format="boolean"/>
<attr name="enable_touch_listener_panel" format="boolean"/>
<attr name="top_fragment_height"
format="dimension"/>
<attr name="x_scale_factor"
format="float"/>
<attr name="y_scale_factor"
format="float"/>
<attr name="top_fragment_margin_right"
format="dimension"/>
<attr name="top_fragment_margin_bottom"
format="dimension"/>
<attr name="enable_horizontal_alpha_effect"
format="boolean"/>
<attr name="enable_click_to_maximize_panel"
format="boolean"/>
<attr name="enable_click_to_minimize_panel"
format="boolean"/>
<attr name="enable_touch_listener_panel"
format="boolean"/>
</declare-styleable>

<declare-styleable name="draggable_view">
<attr name="top_view_id" format="reference"/>
<attr name="bottom_view_id" format="reference"/>
<attr name="top_view_height" format="dimension"/>
<attr name="top_view_x_scale_factor" format="float"/>
<attr name="top_view_y_scale_factor" format="float"/>
<attr name="top_view_margin_right" format="dimension"/>
<attr name="top_view_margin_bottom" format="dimension"/>
<attr name="enable_minimized_horizontal_alpha_effect" format="boolean"/>
<attr name="enable_click_to_maximize_view" format="boolean"/>
<attr name="enable_click_to_minimize_view" format="boolean"/>
<attr name="top_view_resize" format="boolean"/>
<attr name="top_view_id"
format="reference"/>
<attr name="bottom_view_id"
format="reference"/>
<attr name="top_view_height"
format="dimension"/>
<attr name="top_view_x_scale_factor"
format="float"/>
<attr name="top_view_y_scale_factor"
format="float"/>
<attr name="top_view_margin_right"
format="dimension"/>
<attr name="top_view_margin_bottom"
format="dimension"/>
<attr name="enable_minimized_horizontal_alpha_effect"
format="boolean"/>
<attr name="enable_click_to_maximize_view"
format="boolean"/>
<attr name="enable_click_to_minimize_view"
format="boolean"/>
<attr name="top_view_resize"
format="boolean"/>
</declare-styleable>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public void setClickToMinimizeEnabled(boolean enableClickToMinimize) {
}

/**
*
* Slide the view based on scroll of the nav drawer.
* "setEnableTouch" user prevents click to expand while the drawer is moving.
* It's only possible to maximize the view when @slideOffset is equals to 0.0,
Expand Down
72 changes: 54 additions & 18 deletions draggablepanel/src/main/java/com/github/pedrovgs/DraggableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
Expand Down Expand Up @@ -255,7 +256,10 @@ public void minimize() {
* Close the custom view applying an animation to close the view to the right side of the screen.
*/
public void closeToRight() {
if (viewDragHelper.smoothSlideViewTo(dragView, transformer.getOriginalWidth(),

DisplayMetrics dm = getResources().getDisplayMetrics();
int maxSize = Math.max(dm.widthPixels, dm.heightPixels);
if (viewDragHelper.smoothSlideViewTo(dragView, transformer.getOriginalWidth() + maxSize,
getHeight() - transformer.getMinHeightPlusMargin())) {
ViewCompat.postInvalidateOnAnimation(this);
notifyCloseToRightListener();
Expand All @@ -266,7 +270,9 @@ public void closeToRight() {
* Close the custom view applying an animation to close the view to the left side of the screen.
*/
public void closeToLeft() {
if (viewDragHelper.smoothSlideViewTo(dragView, -transformer.getOriginalWidth(),
DisplayMetrics dm = getResources().getDisplayMetrics();
int maxSize = Math.max(dm.widthPixels, dm.heightPixels);
if (viewDragHelper.smoothSlideViewTo(dragView, -maxSize * 20,
getHeight() - transformer.getMinHeightPlusMargin())) {
ViewCompat.postInvalidateOnAnimation(this);
notifyCloseToLeftListener();
Expand Down Expand Up @@ -297,7 +303,7 @@ public boolean isMaximized() {
* @return true if the view is closed at right.
*/
public boolean isClosedAtRight() {
return dragView.getLeft() >= getWidth();
return dragView.getLeft() >= transformer.getOriginalWidth();
}

/**
Expand Down Expand Up @@ -378,7 +384,7 @@ public boolean isClosed() {
}

private void analyzeTouchToMaximizeIfNeeded(MotionEvent ev, boolean isDragViewHit) {
switch(ev.getAction()) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
lastTouchActionDownXPosition = ev.getX();
break;
Expand All @@ -399,8 +405,7 @@ private void analyzeTouchToMaximizeIfNeeded(MotionEvent ev, boolean isDragViewHi

public boolean shouldMaximizeOnClick(MotionEvent ev, float deltaX, boolean isDragViewHit) {
return (Math.abs(deltaX) < MIN_SLIDING_DISTANCE_ON_CLICK)
&& ev.getAction() != MotionEvent.ACTION_MOVE
&& isDragViewHit;
&& ev.getAction() != MotionEvent.ACTION_MOVE && isDragViewHit;
}

/**
Expand All @@ -418,20 +423,52 @@ private MotionEvent cloneMotionEventWithAction(MotionEvent event, int action) {
event.getY(), event.getMetaState());
}

private int oldOrientation = -1;

/**
* 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())
DisplayMetrics dm = getResources().getDisplayMetrics();
if (oldOrientation == -1) {
oldOrientation = getResources().getConfiguration().orientation;
}
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);
ViewHelper.setY(secondView, transformer.getOriginalHeight());
} else {
secondView.layout(left, transformer.getOriginalHeight(), right, bottom);
dragView.setVisibility(INVISIBLE);
if (isClosedAtLeft()) {
if (oldOrientation != getResources().getConfiguration().orientation) {
dragView.layout(left, top, right, transformer.getOriginalHeight());

secondView.layout(left, transformer.getOriginalHeight(), right, bottom);
oldOrientation = getResources().getConfiguration().orientation;
}
closeToLeft();
} else if (isClosedAtRight()) {
if (oldOrientation != getResources().getConfiguration().orientation) {
dragView.layout(left, top, right, transformer.getOriginalHeight());
secondView.layout(left, transformer.getOriginalHeight(), right, bottom);
oldOrientation = getResources().getConfiguration().orientation;
}

closeToRight();
} else {
if (oldOrientation != getResources().getConfiguration().orientation) {
dragView.layout(left, top, right, transformer.getOriginalHeight());
secondView.layout(left, transformer.getOriginalHeight(), right, bottom);
oldOrientation = getResources().getConfiguration().orientation;
}
smoothSlideTo(SLIDE_BOTTOM);
}
dragView.setVisibility(VISIBLE);
}
oldOrientation = getResources().getConfiguration().orientation;
}

/**
Expand Down Expand Up @@ -616,10 +653,8 @@ private boolean isViewHit(View view, int x, int y) {
this.getLocationOnScreen(parentLocation);
int screenX = parentLocation[0] + x;
int screenY = parentLocation[1] + y;
return screenX >= viewLocation[0]
&& screenX < viewLocation[0] + view.getWidth()
&& screenY >= viewLocation[1]
&& screenY < viewLocation[1] + view.getHeight();
return screenX >= viewLocation[0] && screenX < viewLocation[0] + view.getWidth()
&& screenY >= viewLocation[1] && screenY < viewLocation[1] + view.getHeight();
}

/**
Expand All @@ -636,7 +671,8 @@ private void addFragmentToView(final int viewId, final Fragment fragment) {
* Initialize the viewDragHelper.
*/
private void initializeViewDragHelper() {
viewDragHelper = ViewDragHelper.create(this, SENSITIVITY, new DraggableViewCallback(this, dragView));
viewDragHelper =
ViewDragHelper.create(this, SENSITIVITY, new DraggableViewCallback(this, dragView));
}

/**
Expand All @@ -647,8 +683,9 @@ 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,
DEFAULT_TOP_VIEW_HEIGHT));
transformer.setViewHeight(
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));
Expand Down Expand Up @@ -691,9 +728,8 @@ private void initializeAttributes(AttributeSet attrs) {
*/
private boolean smoothSlideTo(float slideOffset) {
final int topBound = getPaddingTop();
int x = (int) (slideOffset * (getWidth() - transformer.getMinWidthPlusMarginRight()));
int y = (int) (topBound + slideOffset * getVerticalDragRange());
if (viewDragHelper.smoothSlideViewTo(dragView, x, y)) {
if (viewDragHelper.smoothSlideViewTo(dragView, 0, y)) {
ViewCompat.postInvalidateOnAnimation(this);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public DraggableViewCallback(DraggableView draggableView, View draggedView) {
@Override public int clampViewPositionHorizontal(View child, int left, int dx) {
int newLeft = draggedView.getLeft();
if ((draggableView.isMinimized() && Math.abs(dx) > MINIMUM_DX_FOR_HORIZONTAL_DRAG) || (
draggableView.isDragViewAtBottom()
&& !draggableView.isDragViewAtRight())) {
draggableView.isDragViewAtBottom() && !draggableView.isDragViewAtRight())) {
newLeft = left;
}
return newLeft;
Expand All @@ -126,13 +125,13 @@ public DraggableViewCallback(DraggableView draggableView, View draggedView) {
*/
@Override public int clampViewPositionVertical(View child, int top, int dy) {
int newTop = draggableView.getHeight() - draggableView.getDraggedViewHeightPlusMarginTop();
if (draggableView.isMinimized() && Math.abs(dy) >= MINIMUM_DY_FOR_VERTICAL_DRAG
|| (!draggableView.isMinimized() && !draggableView.isDragViewAtBottom())) {
if (draggableView.isMinimized() && Math.abs(dy) >= MINIMUM_DY_FOR_VERTICAL_DRAG || (
!draggableView.isMinimized() && !draggableView.isDragViewAtBottom())) {

final int topBound = draggableView.getPaddingTop();
final int bottomBound = draggableView.getHeight()
- draggableView.getDraggedViewHeightPlusMarginTop()
- draggedView.getPaddingBottom();
final int bottomBound =
draggableView.getHeight() - draggableView.getDraggedViewHeightPlusMarginTop()
- draggedView.getPaddingBottom();

newTop = Math.min(Math.max(top, topBound), bottomBound);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,18 @@ class ResizeTransformer extends Transformer {
*/
@Override public void updateScale(float verticalDragOffset) {
layoutParams.width = (int) (getOriginalWidth() * (1 - verticalDragOffset / getXScaleFactor()));
layoutParams.height = (int) (getOriginalHeight() * (1 - verticalDragOffset / getYScaleFactor()));
layoutParams.height =
(int) (getOriginalHeight() * (1 - verticalDragOffset / getYScaleFactor()));

getView().setLayoutParams(layoutParams);
}


/**
* Changes X view position using layout() method.
*
* @param verticalDragOffset used to calculate the new X position.
*/
@Override
public void updatePosition(float verticalDragOffset) {
@Override public void updatePosition(float verticalDragOffset) {
int right = getViewRightPosition(verticalDragOffset);
int left = right - layoutParams.width;
int top = getView().getTop();
Expand All @@ -61,7 +60,6 @@ public void updatePosition(float verticalDragOffset) {
getView().layout(left, top, right, bottom);
}


/**
* @return true if the right position of the view plus the right margin is equals to the parent
* width.
Expand Down Expand Up @@ -116,5 +114,4 @@ public void updatePosition(float verticalDragOffset) {
private int getViewRightPosition(float verticalDragOffset) {
return (int) ((getOriginalWidth()) - getMarginRight() * verticalDragOffset);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,4 @@ class ScaleTransformer extends Transformer {
@Override public int getMinWidthPlusMarginRight() {
return getOriginalWidth();
}

}
Loading