Skip to content

Instantly share code, notes, and snippets.

@lauw
Created April 8, 2015 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lauw/31e2672e69da897586bb to your computer and use it in GitHub Desktop.
Save lauw/31e2672e69da897586bb to your computer and use it in GitHub Desktop.
public class HeroAnimation implements Serializable {
private int mLeft;
private int mTop;
private int mWidth;
private int mHeight;
private float mAlpha;
/**
* @param exampleView View to be animated to with Hero Animation
*/
public HeroAnimation(View exampleView) {
final int[] location = new int[2];
exampleView.getLocationOnScreen(location);
mLeft = location[0];
mTop = location[1];
mWidth = exampleView.getWidth();
mHeight = exampleView.getHeight();
mAlpha = exampleView.getAlpha();
}
public AnimatorSet getAnimations(View targetView, boolean enter) {
if (enter) {
targetView.setPivotX(0);
targetView.setPivotY(0);
}
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
getPositionAnimations(targetView, enter),
getScaleAnimations(targetView, enter),
getAlphaAnimation(targetView, enter)
);
return animatorSet;
}
private AnimatorSet getPositionAnimations(View targetView, boolean enter) {
int[] screenLocation = new int[2];
targetView.getLocationOnScreen(screenLocation);
int leftDelta = mLeft - screenLocation[0];
int topDelta = mTop - screenLocation[1];
float[] xValues = new float[] { leftDelta, 0 };
float[] yValues = new float[] { topDelta, 0 };
if (!enter) {
xValues = new float[] { leftDelta };
yValues = new float[] { topDelta };
}
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
ObjectAnimator.ofFloat(targetView, "translationX", xValues),
ObjectAnimator.ofFloat(targetView, "translationY", yValues)
);
return animatorSet;
}
private AnimatorSet getScaleAnimations(View targetView, boolean enter) {
float widthScale = (float)mWidth / targetView.getWidth();
float heightScale = (float)mHeight / targetView.getHeight();
float[] xValues = new float[] { widthScale, 1 };
float[] yValues = new float[] { heightScale, 1 };
if (!enter) {
xValues = new float[] { widthScale };
yValues = new float[] { heightScale };
}
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
ObjectAnimator.ofFloat(targetView, "scaleX", xValues),
ObjectAnimator.ofFloat(targetView, "scaleY", yValues)
);
return animatorSet;
}
private Animator getAlphaAnimation(View targetView, boolean enter) {
float[] values = new float[] { mAlpha, targetView.getAlpha() };
if (!enter)
values = new float[] { mAlpha };
return ObjectAnimator.ofFloat(targetView, "alpha", values);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment