Skip to content

Instantly share code, notes, and snippets.

@monossido
Created December 2, 2014 23:30
Show Gist options
  • Save monossido/242ec653e7e41cac7665 to your computer and use it in GitHub Desktop.
Save monossido/242ec653e7e41cac7665 to your computer and use it in GitHub Desktop.
Animate a view by scaling in it. Such dialer in Lollipop.Requires API level 21. Credit AOSP.
/**
* * Scales in the view from scale of 0 to actual dimensions.
* * @param view The view to scale.
* * @param durationMs The duration of the scaling in milliseconds.
* * @param startDelayMs The delay to applying the scaling in milliseconds.
*/
public static void scaleIn(final View view, int durationMs, int startDelayMs) {
AnimatorListenerAdapter listener = (new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
view.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationCancel(Animator animation) {
view.setScaleX(1);
view.setScaleY(1);
}
});
scaleInternal(view, 0 /* startScaleValue */, 1 /* endScaleValue */, durationMs,
startDelayMs, listener, new PathInterpolator(0.0f, 0.0f, 0.2f, 1.0f));
}
private static void scaleInternal(final View view, int startScaleValue, int endScaleValue,
int durationMs, int startDelay, AnimatorListenerAdapter listener,
Interpolator interpolator) {
view.setScaleX(startScaleValue);
view.setScaleY(startScaleValue);
final ViewPropertyAnimator animator = view.animate();
animator.cancel();
animator.setInterpolator(interpolator)
.scaleX(endScaleValue)
.scaleY(endScaleValue)
.setListener(listener)
.withLayer();
animator.setDuration(durationMs);
animator.setStartDelay(startDelay);
animator.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment