Skip to content

Commit

Permalink
Added "to()" for the unreveal animation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaouan committed Aug 1, 2016
1 parent 896de0c commit 4ae1b80
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 27 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Android Revealator
An helper to circle reveal/unreveal a view easily, with translations and childs animations.
The libraries is Android 15+ compatible.

![demo](art/demo.gif)
![demo](art/demo1.gif) ![demo](art/demo2.gif)

Installation
--------
Expand All @@ -17,7 +17,7 @@ repositories {
```

```java
compile 'com.github.jaouan:revealator:0.0.1'
compile 'com.github.jaouan:revealator:1.0.0'
```

Usage
Expand Down Expand Up @@ -64,7 +64,9 @@ Revealator.reveal( theAwesomeViewYouWantToReveal )
... or unreveal.
```java
Revealator.unreveal( theAwesomeViewYouWantToUnreveal )
//.withDuration(...)
.to( theInitiatorViewYouWantToTranslateBack )
//.withUnrevealDuration(...)
//.withTranslateDuration(...)
//.withEndAction(...)
.start();
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ public void onClick(View view) {
@Override
public void onClick(View view) {
Revealator.unreveal(theAwesomeView)
.to(fab)
//.withUnrevealDuration(...)
//.withTranslateDuration(...)
.withEndAction(new Runnable() {
@Override
public void run() {
fab.show();
//fab.show();
Snackbar.make(fab, "What a beautiful snackbar !", Snackbar.LENGTH_LONG).show();
}
})
//.withDuration(...)
.start();

}
Expand Down
File renamed without changes
Binary file added art/demo2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 48 additions & 8 deletions revealator/src/main/java/com/jaouan/revealator/Revealator.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,47 @@ public static class UnrevealBuilder {

private View viewToUnreveal;

private int duration = 250;
private int unrevealDuration = 250;

private int translateDuration = 250;

private Runnable endAction;

private View toView;

private UnrevealBuilder(@NonNull final View viewToUnreveal) {
this.viewToUnreveal = viewToUnreveal;
}

/**
* Defines duration.
* Defines the view toView translate after the "unrevealation".
* @param toView View toView translate.
* @return Builder.
*/
public UnrevealBuilder to(final View toView) {
this.toView = toView;
return this;
}

/**
* Defines the unreveal duration.
*
* @param unrevealDuration Unreveal duration.
* @return Builder.
*/
public UnrevealBuilder withUnrevealDuration(final int unrevealDuration) {
this.unrevealDuration = unrevealDuration;
return this;
}

/**
* Defines the translate duration.
*
* @param duration Duration.
* @param translateDuration translate duration.
* @return Builder.
*/
public UnrevealBuilder withDuration(final int duration) {
this.duration = duration;
public UnrevealBuilder withTranslateDuration(final int translateDuration) {
this.translateDuration = translateDuration;
return this;
}

Expand All @@ -215,17 +240,32 @@ public UnrevealBuilder withEndAction(@NonNull final Runnable endAction) {
*/
public void start() {
// - Reveal the view !
RevealatorHelper.unrevealView(viewToUnreveal, this.duration, new Runnable() {
RevealatorHelper.unrevealView(viewToUnreveal, this.unrevealDuration, new Runnable() {
@Override
public void run() {
// - Fire end action if necessary.
if (endAction != null) {
// - If no to view, fire end action if necessary.
if (toView == null && endAction != null) {
endAction.run();
}
}
}
);

// - If to view exists, show and translate the "to view".
if(toView != null) {
RevealatorHelper.showAndTranslateView(toView, viewToUnreveal, (int)(this.unrevealDuration * 0.9f), this.translateDuration, new Runnable() {
@Override
public void run() {
// - Fire end action if necessary.
if (endAction != null) {
endAction.run();
}
}
}
);
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,51 @@
/**
* Helper for the revealator.
*/
public class RevealatorHelper {
public final class RevealatorHelper {

/**
* Helps to translate a view to another view.
* Disallow instantiation.
*/
private RevealatorHelper() {
}

/**
* Get center locations delta between two views.
*
* @param viewA View A.
* @param viewB View B.
* @return Locations delta [X,Y].
*/
private static int[] getCenterLocationsDelta(final View viewA, final View viewB) {
int[] fromLocation = new int[2];
viewA.getLocationOnScreen(fromLocation);
int[] toLocation = new int[2];
viewB.getLocationOnScreen(toLocation);
int deltaX = toLocation[0] - fromLocation[0] + viewB.getMeasuredWidth() / 2 - viewA.getMeasuredWidth() / 2;
int deltaY = toLocation[1] - fromLocation[1] + viewB.getMeasuredHeight() / 2 - viewA.getMeasuredHeight() / 2;
return new int[]{deltaX, deltaY};
}

/**
* Helps to hide then translate a view to another view.
*
* @param fromView From view.
* @param toView Target view.
* @param duration Duration.
*/
static void translateAndHideView(final View fromView, View toView, long duration) {
static void translateAndHideView(final View fromView, final View toView, final long duration) {
// - Determine translate delta.
int[] fromLocation = new int[2];
fromView.getLocationOnScreen(fromLocation);
int[] toLocation = new int[2];
toView.getLocationOnScreen(toLocation);
int toX = toLocation[0] - fromLocation[0] + toView.getMeasuredWidth() / 2 - fromView.getMeasuredWidth() / 2;
int toY = toLocation[1] - fromLocation[1] + toView.getMeasuredHeight() / 2 - fromView.getMeasuredHeight() / 2;
final int[] delta = getCenterLocationsDelta(fromView, toView);

// - Prepare translate animation.
final TranslateAnimation translateAnimation = new TranslateAnimation(0, toX, 0, toY);
final TranslateAnimation translateAnimation = new TranslateAnimation(0, delta[0], 0, delta[1]);
translateAnimation.setDuration(duration);
translateAnimation.setInterpolator(new AccelerateInterpolator());

// - Prepare hide animation.
final ScaleAnimation hideAnimation = new ScaleAnimation(fromView.getScaleX(), 0, fromView.getScaleY(), 0, Animation.ABSOLUTE, toX + fromView.getMeasuredWidth() / 2, Animation.ABSOLUTE, toY + fromView.getMeasuredHeight() / 2);
hideAnimation.setDuration(duration / 5);
hideAnimation.setStartOffset((long) (duration * 0.9));
final ScaleAnimation hideAnimation = new ScaleAnimation(fromView.getScaleX(), 0, fromView.getScaleY(), 0, Animation.ABSOLUTE, delta[0] + fromView.getMeasuredWidth() / 2, Animation.ABSOLUTE, delta[1] + fromView.getMeasuredHeight() / 2);
hideAnimation.setDuration((long) (duration *0.2f));
hideAnimation.setStartOffset((long) (duration * 0.9f));
hideAnimation.setInterpolator(new BounceInterpolator());

// - Prepare animations set.
Expand All @@ -64,6 +82,47 @@ public void onAnimationEnd(Animation animation) {
fromView.startAnimation(animationSet);
}

/**
* 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 animationEndCallBack Callback fired on animation end.
*/
public static void showAndTranslateView(final View viewToTranslate, final View fromView, final int startDelay, final int duration, final Runnable animationEndCallBack) {
// - Determine translate delta.
final int[] delta = getCenterLocationsDelta(viewToTranslate, fromView);

// - Prepare show animation.
final ScaleAnimation showAnimation = new ScaleAnimation(0, viewToTranslate.getScaleX(), 0, viewToTranslate.getScaleY(), Animation.ABSOLUTE, viewToTranslate.getMeasuredWidth() / 2, Animation.ABSOLUTE, viewToTranslate.getMeasuredHeight() / 2);
showAnimation.setDuration((long) (duration * 0.2f));
showAnimation.setInterpolator(new BounceInterpolator());

// - Prepare translate animation.
final TranslateAnimation translateAnimation = new TranslateAnimation(delta[0], 0, delta[1], 0);
translateAnimation.setDuration(duration);
showAnimation.setStartOffset((long) (duration * 0.1f));
translateAnimation.setInterpolator(new AccelerateInterpolator());

// - Prepare animations set.
final AnimationSet animationSet = new AnimationSet(true);
animationSet.setStartOffset(startDelay);
animationSet.addAnimation(showAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.setAnimationListener(new AnimationListenerAdapter() {
@Override
public void onAnimationEnd(Animation animation) {
viewToTranslate.setVisibility(View.VISIBLE);
animationEndCallBack.run();
}
});

// - Let's move !
viewToTranslate.startAnimation(animationSet);
}

/**
* Helps to reveal a view.
*
Expand Down Expand Up @@ -147,7 +206,7 @@ static void findAllVisibleChilds(final ViewGroup viewGroup, final List<View> ord
findAllVisibleChilds((ViewGroup) childView, ordoredChilds);
continue;
}
if(childView.getVisibility() == View.VISIBLE) {
if (childView.getVisibility() == View.VISIBLE) {
ordoredChilds.add(childView);
}
}
Expand Down

0 comments on commit 4ae1b80

Please sign in to comment.