Created
April 18, 2016 12:10
-
-
Save rafakob/a7c4ee74e23055096427c4a20f217645 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Animate { | |
/** | |
* Animate height of a ViewGroup object. | |
* | |
* @param target ViewGroup | |
* @return Builder | |
*/ | |
public static ValueAnimatorBuilder viewGroupHeight(ViewGroup target) { | |
return new ValueAnimatorBuilder<ViewGroup>(target) { | |
@Override | |
protected void onAnimationUpdate(ValueAnimator valueAnimator) { | |
ViewGroup.LayoutParams lp = target.getLayoutParams(); | |
lp.height = (int) valueAnimator.getAnimatedValue(); | |
target.setLayoutParams(lp); | |
} | |
}; | |
} | |
/** | |
* Animate height of a View object. | |
* | |
* @param target View | |
* @return Builder | |
*/ | |
public static ValueAnimatorBuilder viewHeight(View target) { | |
return new ValueAnimatorBuilder<View>(target) { | |
@Override | |
protected void onAnimationUpdate(ValueAnimator valueAnimator) { | |
ViewGroup.LayoutParams lp = target.getLayoutParams(); | |
lp.height = (int) valueAnimator.getAnimatedValue(); | |
target.setLayoutParams(lp); | |
} | |
}; | |
} | |
/** | |
* Animate toolbar expand | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder expandAppBar(AppBarLayout target) { | |
int from = SizeUtils.getThemeDimension(target.getContext(), R.attr.actionBarSize); | |
int to = from + SizeUtils.getThemeDimension(target.getContext(), R.attr.tabLayoutHeight); | |
return Animate.viewGroupHeight(target).withIntValues(from, to).withDuration(200); | |
} | |
/** | |
* Animate toolbar collapse | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder collapseAppBar(AppBarLayout target) { | |
int to = SizeUtils.getThemeDimension(target.getContext(), R.attr.actionBarSize); | |
int from = to + SizeUtils.getThemeDimension(target.getContext(), R.attr.tabLayoutHeight); | |
return Animate.viewGroupHeight(target).withIntValues(from, to).withDuration(200); | |
} | |
/** | |
* Fade in animation | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder fadeIn(final View target) { | |
ValueAnimatorBuilder builder = new ValueAnimatorBuilder<View>(target) { | |
@Override | |
protected void onAnimationUpdate(ValueAnimator valueAnimator) { | |
target.setAlpha((float) valueAnimator.getAnimatedValue()); | |
if (target.getParent() != null) { | |
((View) target.getParent()).postInvalidate(); | |
} | |
} | |
}; | |
if (target.getVisibility() != View.VISIBLE) { | |
target.setAlpha(0); | |
} | |
return builder.withFloatValues(target.getAlpha(), 1) | |
.withDuration((int) (200 * (1 - target.getAlpha()))) | |
.withInterpolator(new DecelerateInterpolator()); | |
} | |
/** | |
* Fade out animation | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder fadeOut(final View target) { | |
ValueAnimatorBuilder builder = new ValueAnimatorBuilder<View>(target) { | |
@Override | |
protected void onAnimationUpdate(ValueAnimator valueAnimator) { | |
target.setAlpha((Float) valueAnimator.getAnimatedValue()); | |
if (target.getParent() != null) { | |
((View) target.getParent()).postInvalidate(); | |
} | |
} | |
}; | |
return builder.withFloatValues(target.getAlpha(), 0) | |
.withDuration((int) (200 * target.getAlpha())) | |
.withInterpolator(new DecelerateInterpolator()); | |
} | |
/** | |
* Pop in animation | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder popIn(final View target) { | |
ValueAnimatorBuilder builder = new ValueAnimatorBuilder<View>(target) { | |
@Override | |
protected void onAnimationUpdate(ValueAnimator valueAnimator) { | |
target.setAlpha((Float) valueAnimator.getAnimatedValue()); | |
target.setScaleX((Float) valueAnimator.getAnimatedValue()); | |
target.setScaleY((Float) valueAnimator.getAnimatedValue()); | |
if (target.getParent() != null) { | |
((View) target.getParent()).postInvalidate(); | |
} | |
} | |
}; | |
if (target.getVisibility() != View.VISIBLE) { | |
target.setAlpha(0); | |
} | |
return builder.withFloatValues(target.getAlpha(), 1) | |
.withDuration((int) (200 * (1 - target.getAlpha()))) | |
.withInterpolator(new DecelerateInterpolator()); | |
} | |
/** | |
* Pop out animation | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder popOut(final View target) { | |
ValueAnimatorBuilder builder = new ValueAnimatorBuilder<View>(target) { | |
@Override | |
protected void onAnimationUpdate(ValueAnimator valueAnimator) { | |
target.setAlpha((Float) valueAnimator.getAnimatedValue()); | |
target.setScaleX((Float) valueAnimator.getAnimatedValue()); | |
target.setScaleY((Float) valueAnimator.getAnimatedValue()); | |
if (target.getParent() != null) { | |
((View) target.getParent()).postInvalidate(); | |
} | |
} | |
}; | |
return builder.withFloatValues(target.getAlpha(), 0) | |
.withDuration((int) (200 * target.getAlpha())) | |
.withInterpolator(new DecelerateInterpolator()); | |
} | |
/** | |
* Fly in animation | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder flyIn(final View target) { | |
ValueAnimatorBuilder builder = new ValueAnimatorBuilder<View>(target) { | |
@Override | |
protected void onAnimationUpdate(ValueAnimator valueAnimator) { | |
target.setAlpha((Float) valueAnimator.getAnimatedValue()); | |
target.setTranslationY(Math.min(target.getHeight() / 2, target.getResources().getDimension(R.dimen.carbon_1dip) * 50.0f) * (1 - (Float) valueAnimator.getAnimatedValue())); | |
if (target.getParent() != null) { | |
((View) target.getParent()).postInvalidate(); | |
} | |
} | |
}; | |
if (target.getVisibility() != View.VISIBLE) { | |
target.setAlpha(0); | |
} | |
return builder.withFloatValues(target.getAlpha(), 1) | |
.withDuration((int) (200 * (1 - target.getAlpha()))) | |
.withInterpolator(new DecelerateInterpolator()); | |
} | |
/** | |
* Fly out animation | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder flyOut(final View target) { | |
ValueAnimatorBuilder builder = new ValueAnimatorBuilder<View>(target) { | |
@Override | |
protected void onAnimationUpdate(ValueAnimator valueAnimator) { | |
target.setAlpha((Float) valueAnimator.getAnimatedValue()); | |
target.setTranslationY(Math.min(target.getHeight() / 2, target.getResources().getDimension(R.dimen.carbon_1dip) * 50.0f) * (1 - (Float) valueAnimator.getAnimatedValue())); | |
if (target.getParent() != null) { | |
((View) target.getParent()).postInvalidate(); | |
} | |
} | |
}; | |
return builder.withFloatValues(target.getAlpha(), 0) | |
.withDuration((int) (200 * target.getAlpha())) | |
.withInterpolator(new DecelerateInterpolator()); | |
} | |
/** | |
* BrightnessSaturationFadeIn | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder brightnessSaturationFadeIn(final ImageView target) { | |
final AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator(); | |
ValueAnimatorBuilder builder = new ValueAnimatorBuilder<ImageView>(target) { | |
ColorMatrix saturationMatrix = new ColorMatrix(); | |
ColorMatrix brightnessMatrix = new ColorMatrix(); | |
@Override | |
public void onAnimationUpdate(ValueAnimator valueAnimator) { | |
float fraction = animator.getAnimatedFraction(); | |
saturationMatrix.setSaturation((Float) animator.getAnimatedValue()); | |
float scale = 2 - interpolator.getInterpolation(Math.min(fraction * 4 / 3, 1)); | |
brightnessMatrix.setScale(scale, scale, scale, interpolator.getInterpolation(Math.min(fraction * 2, 1))); | |
saturationMatrix.preConcat(brightnessMatrix); | |
target.setColorFilter(new ColorMatrixColorFilter(saturationMatrix)); | |
if (target.getParent() != null) { | |
((View) target.getParent()).postInvalidate(); | |
} | |
} | |
}; | |
return builder.withFloatValues(0, 1) | |
.withDuration(800) | |
.withInterpolator(interpolator); | |
} | |
/** | |
* BrightnessSaturationFadeOut | |
* | |
* @param target | |
* @return | |
*/ | |
public static ValueAnimatorBuilder brightnessSaturationFadeOut(final ImageView target) { | |
final AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator(); | |
ValueAnimatorBuilder builder = new ValueAnimatorBuilder<ImageView>(target) { | |
ColorMatrix saturationMatrix = new ColorMatrix(); | |
ColorMatrix brightnessMatrix = new ColorMatrix(); | |
@Override | |
public void onAnimationUpdate(ValueAnimator valueAnimator) { | |
float fraction = animator.getAnimatedFraction(); | |
saturationMatrix.setSaturation((Float) animator.getAnimatedValue()); | |
float scale = 2 - interpolator.getInterpolation(Math.min((1 - fraction) * 4 / 3, 1)); | |
brightnessMatrix.setScale(scale, scale, scale, interpolator.getInterpolation(Math.min((1 - fraction) * 2, 1))); | |
saturationMatrix.preConcat(brightnessMatrix); | |
target.setColorFilter(new ColorMatrixColorFilter(saturationMatrix)); | |
if (target.getParent() != null) { | |
((View) target.getParent()).postInvalidate(); | |
} | |
} | |
}; | |
return builder.withFloatValues(1, 0) | |
.withDuration(800) | |
.withInterpolator(interpolator); | |
} | |
public static float lerp(float interpolation, float val1, float val2) { | |
return val1 * (1 - interpolation) + val2 * interpolation; | |
} | |
public static int lerpColor(float interpolation, int val1, int val2) { | |
int a = (int) lerp(interpolation, val1 >> 24, val2 >> 24); | |
int r = (int) lerp(interpolation, (val1 >> 16) & 0xff, (val2 >> 16) & 0xff); | |
int g = (int) lerp(interpolation, (val1 >> 8) & 0xff, (val2 >> 8) & 0xff); | |
int b = (int) lerp(interpolation, val1 & 0xff, val2 & 0xff); | |
return Color.argb(a, r, g, b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment