Skip to content

Commit

Permalink
Added curve customization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaouan committed Aug 14, 2016
1 parent 05f9daf commit b9d51b0
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 23 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ Just put the view you want to animate in a `io.codetail.widget.RevealFrameLayout
Revealator.reveal( theAwesomeViewYouWantToReveal )
.from( theInitiatorViewYouWantToTranslate )
.withCurvedTranslation()
//.withCurvedTranslation(curvePoint)
.withChildsAnimation()
//.withDelayBetweenChildAnimation(...)
//.withChildAnimationDuration(...)
//.withTranslateDuration(...)
//.withHideFromViewAtTranslateInterpolatedTime(...)
//.withRevealDuration(...)
//.withEndAction(...)
.start();
Expand All @@ -68,8 +70,10 @@ Revealator.reveal( theAwesomeViewYouWantToReveal )
Revealator.unreveal( theAwesomeViewYouWantToUnreveal )
.to( theInitiatorViewYouWantToTranslateBack )
.withCurvedTranslation()
//.withCurvedTranslation(curvePoint)
//.withUnrevealDuration(...)
//.withTranslateDuration(...)
//.withShowFromViewInterpolatedDuration(...)
//.withEndAction(...)
.start();
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jaouan.revealator;

import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -28,8 +29,12 @@ public class RevealBuilder {

private boolean mCurvedTranslation = false;

private PointF mCurveControlPoint;

private Runnable mEndAction;

private float mHideFromViewAtInterpolatedTime = .8f;

/**
* Reveal builder's contructor.
*
Expand Down Expand Up @@ -61,6 +66,17 @@ public RevealBuilder withTranslateDuration(final int translateDuration) {
return this;
}

/**
* Defines when from view starts to hide.
*
* @param hideFromViewAtInterpolatedTime End from view at interpolated time of translation. Must be between 0 and 1. (default : 0.8f)
* @return Builder.
*/
public RevealBuilder withHideFromViewAtTranslateInterpolatedTime(final float hideFromViewAtInterpolatedTime) {
this.mHideFromViewAtInterpolatedTime = hideFromViewAtInterpolatedTime;
return this;
}

/**
* Defines that childs should be animated after reveal.
*
Expand All @@ -81,6 +97,18 @@ public RevealBuilder withCurvedTranslation() {
return this;
}


/**
* Defines that translation must be curved.
*
* @param curveControlPoint Relative curved control point.
* @return Builder.
*/
public RevealBuilder withCurvedTranslation(final PointF curveControlPoint) {
this.mCurveControlPoint = curveControlPoint;
return this.withCurvedTranslation();
}

/**
* Defines by child animation duration.
*
Expand Down Expand Up @@ -135,7 +163,7 @@ public void start() {
// - If from view exists, translate and hide the "from view" and delay reveal animation.
int revealStartDelay = 0;
if (this.mFromView != null) {
RevealatorHelper.translateAndHideView(this.mFromView, this.mViewToReveal, this.mTranslateDuration, this.mCurvedTranslation);
RevealatorHelper.translateAndHideView(this.mFromView, this.mViewToReveal, this.mTranslateDuration, this.mCurvedTranslation, this.mCurveControlPoint, this.mHideFromViewAtInterpolatedTime);
revealStartDelay = (int) (this.mTranslateDuration * 0.9f);
}

Expand Down Expand Up @@ -163,4 +191,5 @@ public void run() {
}
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@ private RevealatorHelper() {
/**
* Helps to hide then translate a view to another view.
*
* @param fromView From view.
* @param toView Target view.
* @param duration Duration.
* @param curvedTranslation Curved translation.
* @param fromView From view.
* @param toView Target view.
* @param duration Duration.
* @param curvedTranslation Curved translation.
* @param controlPoint Curved angle.
* @param hideFromViewAtInterpolatedTime Start hiding from view interpolated time. Must be between 0 and 1.
*/
static void translateAndHideView(final View fromView, final View toView, final long duration, final boolean curvedTranslation) {
static void translateAndHideView(final View fromView, final View toView, final long duration, final boolean curvedTranslation, final PointF controlPoint, final float hideFromViewAtInterpolatedTime) {
// - Determine translate delta.
final PointF delta = getCenterLocationsDelta(fromView, toView);

// - Prepare translate animation.
Animation translateAnimation;
if (curvedTranslation) {
translateAnimation = new BezierTranslateAnimation(0, delta.x, 0, delta.y);
translateAnimation = new BezierTranslateAnimation(0, delta.x, 0, delta.y, controlPoint);
} else {
translateAnimation = new TranslateAnimation(0, delta.x, 0, delta.y);
}
Expand All @@ -56,9 +58,9 @@ static void translateAndHideView(final View fromView, final View toView, final l

// - Prepare hide animation.
final ScaleAnimation hideAnimation = new ScaleAnimation(fromView.getScaleX(), 0, fromView.getScaleY(), 0, Animation.ABSOLUTE, delta.x + fromView.getMeasuredWidth() / 2, Animation.ABSOLUTE, delta.y + fromView.getMeasuredHeight() / 2);
hideAnimation.setDuration((long) (duration * 0.2f));
hideAnimation.setStartOffset((long) (duration * 0.9f));
hideAnimation.setInterpolator(new BounceInterpolator());
hideAnimation.setDuration((long) (duration * Math.min(1, Math.max(0, 1 - hideFromViewAtInterpolatedTime))));
hideAnimation.setStartOffset((long) (duration * Math.min(1, Math.max(0, hideFromViewAtInterpolatedTime))));
hideAnimation.setInterpolator(new AccelerateInterpolator());

// - Prepare animations set.
final AnimationSet animationSet = new AnimationSet(true);
Expand All @@ -78,26 +80,27 @@ public void onAnimationEnd(Animation animation) {
/**
* Helps to translate then show a view to another view.
*
* @param viewToTranslate View to translate..
* @param fromView From view.
* @param startDelay Start delay.
* @param duration Translate duration.
* @param curvedTranslation Curved translation.
* @param animationEndCallBack Callback fired on animation end.
* @param viewToTranslate View to translate..
* @param fromView From view.
* @param startDelay Start delay.
* @param duration Translate duration.
* @param curvedTranslation Curved translation.
* @param controlPoint Curved angle.
* @param showFromViewInterpolatedDuration Show from view interpolated duration. Must be between 0 and 1.
*/
public static void showAndTranslateView(final View viewToTranslate, final View fromView, final int startDelay, final int duration, final boolean curvedTranslation, final Runnable animationEndCallBack) {
public static void showAndTranslateView(final View viewToTranslate, final View fromView, final int startDelay, final int duration, final boolean curvedTranslation, final PointF controlPoint, float showFromViewInterpolatedDuration, final Runnable animationEndCallBack) {
// - Determine translate delta.
final PointF delta = getCenterLocationsDelta(viewToTranslate, fromView);

// - Prepare show animation.
final ScaleAnimation showAnimation = new ScaleAnimation(0, viewToTranslate.getScaleX(), 0, viewToTranslate.getScaleY(), Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
showAnimation.setDuration((long) (duration * 0.2f));
showAnimation.setDuration((long) (duration * Math.min(1, Math.max(0, showFromViewInterpolatedDuration))));
showAnimation.setInterpolator(new BounceInterpolator());

// - Prepare translate animation.
Animation translateAnimation;
if (curvedTranslation) {
translateAnimation = new BezierTranslateAnimation(delta.x, 0, delta.y, 0);
translateAnimation = new BezierTranslateAnimation(delta.x, 0, delta.y, 0, controlPoint);
} else {
translateAnimation = new TranslateAnimation(delta.x, 0, delta.y, 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jaouan.revealator;

import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.view.View;

Expand All @@ -14,10 +15,13 @@ public class UnrevealBuilder {

private int mTranslateDuration = 250;

private float mShowFromViewInterpolatedDuration = 0.2f;

private boolean mCurvedTranslation = false;

private Runnable mEndAction;
private PointF mCurveControlPoint;

private Runnable mEndAction;
private View mToView;

/**
Expand Down Expand Up @@ -62,6 +66,17 @@ public UnrevealBuilder withTranslateDuration(final int translateDuration) {
return this;
}

/**
* Defines from view's showing animation interpolated duration.
*
* @param showFromViewInterpolatedDuration Ends showing from view interpolated duration. Must be between 0 and 1. (default : 0.2f)
* @return Builder.
*/
public UnrevealBuilder withShowFromViewInterpolatedDuration(final float showFromViewInterpolatedDuration) {
this.mShowFromViewInterpolatedDuration = showFromViewInterpolatedDuration;
return this;
}


/**
* Defines that translation must be curved.
Expand All @@ -73,6 +88,17 @@ public UnrevealBuilder withCurvedTranslation() {
return this;
}

/**
* Defines that translation must be curved.
*
* @param curveControlPoint Relative curved control point.
* @return Builder.
*/
public UnrevealBuilder withCurvedTranslation(final PointF curveControlPoint) {
this.mCurveControlPoint = curveControlPoint;
return this.withCurvedTranslation();
}

/**
* Defines end action callback.
*
Expand Down Expand Up @@ -102,7 +128,7 @@ public void run() {

// - If to view exists, show and translate the "to view".
if (this.mToView != null) {
RevealatorHelper.showAndTranslateView(this.mToView, this.mViewToUnreveal, (int) (this.mUnrevealDuration * 0.9f), this.mTranslateDuration, this.mCurvedTranslation, new Runnable() {
RevealatorHelper.showAndTranslateView(this.mToView, this.mViewToUnreveal, (int) (this.mUnrevealDuration * 0.9f), this.mTranslateDuration, this.mCurvedTranslation, this.mCurveControlPoint, this.mShowFromViewInterpolatedDuration, new Runnable() {
@Override
public void run() {
// - Fire end action if necessary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ public BezierTranslateAnimation(int fromXType, float fromXValue, int toXType,
mToYType = toYType;
}


/**
* Constructor to use when building a BezierTranslateAnimation from code.
*
* @param fromXDelta Change in X coordinate to apply at the start of the animation
* @param toXDelta Change in X coordinate to apply at the end of the animation
* @param fromYDelta Change in Y coordinate to apply at the start of the animation
* @param toYDelta Change in Y coordinate to apply at the end of the animation
* @param controlPoint Control point for Bezier algorithm.
*/
public BezierTranslateAnimation(float fromXDelta, float toXDelta,
float fromYDelta, float toYDelta,
PointF controlPoint) {
this(fromXDelta, toXDelta, fromYDelta, toYDelta);
mControl = controlPoint;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float dx = calculateBezier(interpolatedTime, mStart.x, mControl.x, mEnd.x);
Expand All @@ -100,8 +117,10 @@ public void initialize(int width, int height, int parentWidth,

mStart = new PointF(mFromXDelta, mFromYDelta);
mEnd = new PointF(mToXDelta, mToYDelta);
// - Define the cross of the two tangents from point 0 and point 1 as control point.
mControl = new PointF(mFromXDelta, mToYDelta);
// - Define the cross of the two tangents from point 0 and point 1 as control point if necessary.
if (mControl == null) {
mControl = new PointF(mFromXDelta, mToYDelta);
}
}

/**
Expand Down

0 comments on commit b9d51b0

Please sign in to comment.