Skip to content

Instantly share code, notes, and snippets.

@VladSumtsov
Created July 2, 2015 09:46
Show Gist options
  • Save VladSumtsov/c452b1a2b08a8b6478d6 to your computer and use it in GitHub Desktop.
Save VladSumtsov/c452b1a2b08a8b6478d6 to your computer and use it in GitHub Desktop.
Reveal animation for using from float button
package com.corewillsoft.loansdeposits.ui.widget;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.internal.widget.ThemeUtils;
import android.util.AttributeSet;
import android.view.View;
import com.corewillsoft.loansdeposits.R;
public class RevealCircleView extends View {
private AnimatorSet animatorSet;
private float maxScale;
private float minScale;
private int animationDuration;
public RevealCircleView(Context context) {
super(context);
init();
}
public RevealCircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RevealCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public RevealCircleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
GradientDrawable drawable = (GradientDrawable) getResources().getDrawable(R.drawable.reveal_circle);
drawable.setColor(ThemeUtils.getThemeAttrColor(getContext(), R.attr.colorAccent));
setBackgroundDrawable(drawable);
animationDuration = getResources().getInteger(R.integer.animation_duration);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
if (parentWidth > parentHeight) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
} else {
super.onMeasure(heightMeasureSpec, heightMeasureSpec);
}
}
public void startAnimation(int x, int y) {
if (animatorSet != null && animatorSet.isRunning()) {
return;
}
maxScale = getMaxScale(x, y);
minScale = (float) getResources().getDimensionPixelOffset(R.dimen.enter_button_size) / (float) getWidth();
setTranslationX(x - getWidth() / 2);
setTranslationY(y - getHeight() / 2);
ObjectAnimator.ofFloat(this, ALPHA, 0, 1).setDuration(40).start();
animatorSet = new AnimatorSet();
animatorSet.playTogether(
ObjectAnimator.ofFloat(this, SCALE_X, minScale, maxScale),
ObjectAnimator.ofFloat(this, SCALE_Y, minScale, maxScale));
animatorSet.setDuration(animationDuration);
animatorSet.start();
}
public void endAnimation(AnimatorListener listener, int delay) {
AnimatorSet revealSet = new AnimatorSet();
revealSet.playTogether(
ObjectAnimator.ofFloat(this, SCALE_X, maxScale, minScale),
ObjectAnimator.ofFloat(this, SCALE_Y, maxScale, minScale));
revealSet.setDuration(animationDuration);
Animator fadeout = ObjectAnimator.ofFloat(this, View.ALPHA, 1, 0);
fadeout.setDuration(150);
animatorSet = new AnimatorSet();
animatorSet.playSequentially(revealSet, fadeout);
animatorSet.addListener(listener);
animatorSet.setStartDelay(delay);
animatorSet.start();
}
private float getMaxScale(int x, int y) {
return (float) (Math.sqrt(x * x + y * y) / (float) getWidth()) * 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment