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

Commit

Permalink
Merge pull request #27 from Fiddl3/ResizeTransformer_fix
Browse files Browse the repository at this point in the history
Resize transformer fix.
  • Loading branch information
pedrovgs committed Dec 6, 2014
2 parents 1021337 + 43d0a1b commit d172e50
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 210 deletions.
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,6 @@ Do you want to resize the top view instead of scale it? Add ``dragable_view:top_
draggable_view:top_view_resize="true">
```

Building the sample app
-----------------------

youtube-android-player-api is required to successfully build and open the sample app in Android Studio.
Unfortunately this library has not been published to Maven central, so you will need to download and deploy it locally.
The [mvn-install-youtubeplayerapi.sh script][19] can be used to easily do this with the following three commands:

wget https://raw.githubusercontent.com/wuman/youtube-android-player-mvn-install/master/mvn-install-youtubeplayerapi.sh
chmod +x mvn-install-youtubeplayerapi.sh
./mvn-install-youtubeplayerapi.sh


Do you want to contribute? TODO
-------------------------------
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
classpath 'com.android.tools.build:gradle:0.12.2'
}
}

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
*/
public class DraggablePanel extends FrameLayout {

private static final float DEFAULT_TOP_FRAGMENT_HEIGHT = 200;
private static final int DEFAULT_TOP_FRAGMENT_HEIGHT = 200;
private static final int DEFAULT_TOP_FRAGMENT_MARGIN = 0;
private static final float DEFAULT_SCALE_FACTOR = 2;
private static final float DEFAULT_TOP_FRAGMENT_MARGIN = 0;
private static final boolean DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT = true;
private static final boolean DEFAULT_TOP_FRAGMENT_RESIZE = false;

Expand All @@ -46,9 +46,9 @@ public class DraggablePanel extends FrameLayout {
private FragmentManager fragmentManager;
private Fragment topFragment;
private Fragment bottomFragment;
private float topFragmentHeight;
private float topFragmentMarginRight;
private float topFragmentMarginBottom;
private int topFragmentHeight;
private int topFragmentMarginRight;
private int topFragmentMarginBottom;
private float xScaleFactor;
private float yScaleFactor;
private boolean enableHorizontalAlphaEffect;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void setBottomFragment(Fragment bottomFragment) {
*
* @param topFragmentHeight in pixels.
*/
public void setTopViewHeight(float topFragmentHeight) {
public void setTopViewHeight(int topFragmentHeight) {
this.topFragmentHeight = topFragmentHeight;
}

Expand All @@ -124,7 +124,7 @@ public void setYScaleFactor(float yScaleFactor) {
*
* @param topFragmentMarginRight in pixels.
*/
public void setTopFragmentMarginRight(float topFragmentMarginRight) {
public void setTopFragmentMarginRight(int topFragmentMarginRight) {
this.topFragmentMarginRight = topFragmentMarginRight;
}

Expand All @@ -133,7 +133,7 @@ public void setTopFragmentMarginRight(float topFragmentMarginRight) {
*
* @param topFragmentMarginBottom in pixels.
*/
public void setTopFragmentMarginBottom(float topFragmentMarginBottom) {
public void setTopFragmentMarginBottom(int topFragmentMarginBottom) {
this.topFragmentMarginBottom = topFragmentMarginBottom;
}

Expand Down Expand Up @@ -253,17 +253,17 @@ public boolean isClosedAtLeft() {
private void initializeAttrs(AttributeSet attrs) {
TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.draggable_panel);
this.topFragmentHeight =
attributes.getDimension(R.styleable.draggable_panel_top_fragment_height,
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.getDimension(R.styleable.draggable_panel_top_fragment_margin_right,
attributes.getDimensionPixelSize(R.styleable.draggable_panel_top_fragment_margin_right,
DEFAULT_TOP_FRAGMENT_MARGIN);
this.topFragmentMarginBottom =
attributes.getDimension(R.styleable.draggable_panel_top_fragment_margin_bottom,
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,
Expand Down
45 changes: 18 additions & 27 deletions draggablepanel/src/main/java/com/github/pedrovgs/DraggableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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 float DEFAULT_TOP_VIEW_HEIGHT = -1;
private static final int DEFAULT_TOP_VIEW_HEIGHT = -1;
private static final float SLIDE_TOP = 0f;
private static final float SLIDE_BOTTOM = 1f;
private static final boolean DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT = true;
Expand Down Expand Up @@ -96,14 +96,14 @@ public void setYTopViewScaleFactor(float yScaleFactor) {
*
* @param topFragmentMarginRight in pixels.
*/
public void setTopViewMarginRight(float topFragmentMarginRight) {
public void setTopViewMarginRight(int topFragmentMarginRight) {
transformer.setMarginRight(topFragmentMarginRight);
}

/**
* Configure the dragView margin bottom applied when the dragView is minimized.
*/
public void setTopViewMarginBottom(float topFragmentMarginBottom) {
public void setTopViewMarginBottom(int topFragmentMarginBottom) {
transformer.setMarginBottom(topFragmentMarginBottom);
}

Expand Down Expand Up @@ -290,13 +290,15 @@ 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) {
int newTop = (int) transformer.getViewHeight();
int lastTopPosition = transformer.getLastTopPosition();
int lastLeftPosition = transformer.getLastLeftPosition();
int newRight = transformer.getLastRightPosition();
int newBottom = lastTopPosition + newTop;
dragView.layout(lastLeftPosition, lastTopPosition, newRight, newBottom);
secondView.layout(0, (int) transformer.getOriginalHeight(), right, getHeight());
if (isInEditMode())
super.onLayout(changed, left, top, right, bottom);
else if (isDragViewAtTop()) {
dragView.layout(left, top, right, (int) transformer.getOriginalHeight());
secondView.layout(left, (int) transformer.getOriginalHeight(), right, bottom);

ViewHelper.setY(dragView, top);
ViewHelper.setY(secondView, transformer.getOriginalHeight());
}
}

/**
Expand Down Expand Up @@ -348,22 +350,12 @@ void attachBottomFragment(Fragment bottomFragment) {
addFragmentToView(R.id.second_view, bottomFragment);
}

/**
* Update the last top and left position when the view is dragged. This last positions are used
* to recreate the dragged view and secondView positions if the requestLayout method is called.
*/
void updateLastDragViewPosition(int lastTopPosition, int lastLeftPosition) {
transformer.setLastTopPosition(lastTopPosition);
transformer.setLastLeftPosition(lastLeftPosition);
}

/**
* Modify dragged view pivot based on the dragged view vertical position to simulate a horizontal
* displacement while the view is dragged.
*/
void changeDragViewPosition() {
transformer.updateXPosition(getVerticalDragOffset());
transformer.updateYPosition(getVerticalDragOffset());
transformer.updatePosition(getVerticalDragOffset());
}

/**
Expand All @@ -377,8 +369,7 @@ void changeSecondViewPosition() {
* Modify dragged view scale based on the dragged view vertical position and the scale factor.
*/
void changeDragViewScale() {
transformer.updateWidth(getVerticalDragOffset());
transformer.updateHeight(getVerticalDragOffset());
transformer.updateScale(getVerticalDragOffset());
}

/**
Expand Down Expand Up @@ -524,7 +515,7 @@ 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.getDimension(R.styleable.draggable_view_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,
Expand All @@ -533,10 +524,10 @@ private void initializeTransformer(TypedArray attributes) {
attributes.getFloat(R.styleable.draggable_view_top_view_y_scale_factor,
DEFAULT_SCALE_FACTOR));
transformer.setMarginRight(
attributes.getDimension(R.styleable.draggable_view_top_view_margin_right,
attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_margin_right,
DEFAULT_TOP_VIEW_MARGIN));
transformer.setMarginBottom(
attributes.getDimension(R.styleable.draggable_view_top_view_margin_bottom,
attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_margin_bottom,
DEFAULT_TOP_VIEW_MARGIN));
}

Expand All @@ -562,7 +553,7 @@ private void initializeAttributes(AttributeSet attrs) {
*/
private boolean smoothSlideTo(float slideOffset) {
final int topBound = getPaddingTop();
int x = (int) (slideOffset * (getWidth() - transformer.getMinWidth()));
int x = (int) (slideOffset * (getWidth() - transformer.getMinWidthPlusMarginRight()));
int y = (int) (topBound + slideOffset * getVerticalDragRange());
if (viewDragHelper.smoothSlideViewTo(dragView, x, y)) {
ViewCompat.postInvalidateOnAnimation(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
*/
class DraggableViewCallback extends ViewDragHelper.Callback {

private static final int MINIMUN_DX_FOR_HORIZONTAL_DRAG = 5;
private static final int MINIMUM_DX_FOR_HORIZONTAL_DRAG = 5;
private static final int MINIMUM_DY_FOR_VERTICAL_DRAG = 15;
private static final float X_MIN_VELOCITY = 1300;
private static final float Y_MIN_VELOCITY = 1300;
private static final float X_MIN_VELOCITY = 1500;
private static final float Y_MIN_VELOCITY = 1000;

private DraggableView draggableView;
private View draggedView;
Expand All @@ -54,7 +54,7 @@ public DraggableViewCallback(DraggableView draggableView, View draggedView) {
* @param dy change in Y position from the last call.
*/
@Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
draggableView.updateLastDragViewPosition(top, left);

if (draggableView.isDragViewAtBottom()) {
draggableView.changeDragViewViewAlpha();
} else {
Expand All @@ -65,6 +65,9 @@ public DraggableViewCallback(DraggableView draggableView, View draggedView) {
draggableView.changeSecondViewPosition();
draggableView.changeBackgroundAlpha();
}
if (0 == top) {
draggableView.requestLayout();
}
}

/**
Expand Down Expand Up @@ -108,7 +111,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) > MINIMUN_DX_FOR_HORIZONTAL_DRAG) || (
if ((draggableView.isMinimized() && Math.abs(dx) > MINIMUM_DX_FOR_HORIZONTAL_DRAG) || (
draggableView.isDragViewAtBottom()
&& !draggableView.isDragViewAtRight())) {
newLeft = left;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,66 +26,41 @@
*/
class ResizeTransformer extends Transformer {

private float lastHeight;
private final RelativeLayout.LayoutParams layoutParams;

ResizeTransformer(View view, View parent) {
super(view, parent);
layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
}

/**
* Changes view width using view's LayoutParam.
* Changes view scale using view's LayoutParam.
*
* @param verticalDragOffset used to calculate the new width.
* @param verticalDragOffset used to calculate the new size.
*/
@Override public void updateWidth(float verticalDragOffset) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getView().getLayoutParams();
int newWidth = (int) (getOriginalWidth() * (1 - verticalDragOffset / getXScaleFactor()));
params.width = newWidth;
setLastLeftPosition((int) (getOriginalWidth() - newWidth));
getView().setLayoutParams(params);
}
@Override public void updateScale(float verticalDragOffset) {
layoutParams.width = (int) (getOriginalWidth() * (1 - verticalDragOffset / getXScaleFactor()));
layoutParams.height = (int) (getOriginalHeight() * (1 - verticalDragOffset / getYScaleFactor()));

/**
* Changes view height using view's LayoutParam.
*
* @param verticalDragOffset used to calculate the new width.
*/
@Override public void updateHeight(float verticalDragOffset) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getView().getLayoutParams();
int newHeight = (int) (getOriginalHeight() * (1 - verticalDragOffset / getYScaleFactor()));
params.height = newHeight;
lastHeight = newHeight;
getView().setLayoutParams(params);
getView().setLayoutParams(layoutParams);
}

/**
* @return last updated height or the view height if the view hasn't been changed yet.
*/
@Override public float getViewHeight() {
return lastHeight == 0 ? super.getViewHeight() : lastHeight;
}

/**
* Changes X view position using layout() method.
*
* @param verticalDragOffset used to calculate the new X position.
*/
@Override public void updateXPosition(float verticalDragOffset) {
int left, top, right, bottom;
left = (int) (getOriginalWidth() - getViewWidth());
right = getViewRightPosition(verticalDragOffset);
setLastRightPosition(right);
top = getView().getTop();
bottom = getView().getBottom();
@Override
public void updatePosition(float verticalDragOffset) {
int right = getViewRightPosition(verticalDragOffset);
int left = right - layoutParams.width;
int top = getView().getTop();
int bottom = top + layoutParams.height;

getView().layout(left, top, right, bottom);
}

/**
* Empty implementation. ViewDragHelper already changes the Y position.
*/
@Override public void updateYPosition(float verticalDragOffset) {
// Empty
}

/**
* @return true if the right position of the view plus the right margin is equals to the parent
Expand Down Expand Up @@ -116,21 +91,21 @@ class ResizeTransformer extends Transformer {
* the parent width.
*/
@Override public boolean isNextToLeftBound() {
return (getView().getLeft() - getMarginRight()) < getParentView().getWidth() * 0.25;
return (getView().getLeft() - getMarginRight()) < getParentView().getWidth() * 0.05;
}

/**
* Uses the Y scale factor to calculate the min possible height.
*/
@Override public int getMinHeightPlusMargin() {
return (int) ((getOriginalHeight() / getYScaleFactor()) + getMarginBottom());
return (int) (getOriginalHeight() * (1 - 1 / getYScaleFactor()) + getMarginBottom());
}

/**
* Uses the X scale factor to calculate the min possible width.
*/
@Override public int getMinWidth() {
return (int) (getOriginalWidth() / getXScaleFactor());
@Override public int getMinWidthPlusMarginRight() {
return (int) (getOriginalWidth() * (1 - 1 / getXScaleFactor()) + getMarginRight());
}

/**
Expand All @@ -141,4 +116,5 @@ class ResizeTransformer extends Transformer {
private int getViewRightPosition(float verticalDragOffset) {
return (int) ((getOriginalWidth()) - getMarginRight() * verticalDragOffset);
}

}
Loading

0 comments on commit d172e50

Please sign in to comment.