From 16cafd28a972b30723b6376f5a8de417ca1ea37b Mon Sep 17 00:00:00 2001 From: Alan Tai Date: Mon, 4 Sep 2017 17:04:10 +0800 Subject: [PATCH] #38 Disable click during animation (#39) Will need some improvements in the code though. --- .../com/joaquimley/faboptions/FabOptions.java | 82 +++++++++++++++---- 1 file changed, 66 insertions(+), 16 deletions(-) diff --git a/faboptions/src/main/java/com/joaquimley/faboptions/FabOptions.java b/faboptions/src/main/java/com/joaquimley/faboptions/FabOptions.java index 8ae3338..b7575c7 100644 --- a/faboptions/src/main/java/com/joaquimley/faboptions/FabOptions.java +++ b/faboptions/src/main/java/com/joaquimley/faboptions/FabOptions.java @@ -37,6 +37,7 @@ import android.support.v7.widget.AppCompatImageView; import android.transition.ChangeBounds; import android.transition.ChangeTransform; +import android.transition.Transition; import android.transition.TransitionManager; import android.transition.TransitionSet; import android.util.AttributeSet; @@ -60,6 +61,7 @@ public class FabOptions extends FrameLayout implements View.OnClickListener { private static final int NO_DIMENSION = 0; private static final long CLOSE_MORPH_TRANSFORM_DURATION = 70; + private boolean mIsAnimating; private boolean mIsOpen; private View.OnClickListener mListener; @@ -221,16 +223,20 @@ private boolean styleButton(int buttonIndex, @ColorRes int color) { @Override public void onClick(View v) { - if (v.getId() == R.id.faboptions_fab) { - if (mIsOpen) { - close(); + if (!mIsAnimating) { + mIsAnimating = true; + + if (v.getId() == R.id.faboptions_fab) { + if (mIsOpen) { + close(); + } else { + open(); + } } else { - open(); - } - } else { - if (mListener != null && mIsOpen) { - mListener.onClick(v); - close(); + if (mListener != null && mIsOpen) { + mListener.onClick(v); + close(); + } } } } @@ -251,13 +257,35 @@ private void open() { } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - TransitionManager.beginDelayedTransition(this, new OpenMorphTransition(mButtonContainer)); + final TransitionSet transitionSet = new OpenMorphTransition(mButtonContainer); + transitionSet.addListener(new Transition.TransitionListener() { + @Override + public void onTransitionStart(final Transition transition) { + } + + @Override + public void onTransitionEnd(final Transition transition) { + mIsAnimating = false; + } + + @Override + public void onTransitionCancel(final Transition transition) { + } + + @Override + public void onTransitionPause(final Transition transition) { + } + + @Override + public void onTransitionResume(final Transition transition) { + } + }); + + TransitionManager.beginDelayedTransition(this, transitionSet); } animateBackground(true); animateButtons(true); -// } else { -// openCompatAnimation(); -// } + mIsOpen = true; } @@ -270,9 +298,31 @@ private void close() { mFab.setImageResource(R.drawable.faboptions_ic_overflow); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - TransitionManager.beginDelayedTransition(this, new CloseMorphTransition(mButtonContainer)); -// } else { -// closeCompatAnimation(); + final TransitionSet transitionSet = new CloseMorphTransition(mButtonContainer); + transitionSet.addListener(new Transition.TransitionListener() { + @Override + public void onTransitionStart(final Transition transition) { + } + + @Override + public void onTransitionEnd(final Transition transition) { + mIsAnimating = false; + } + + @Override + public void onTransitionCancel(final Transition transition) { + } + + @Override + public void onTransitionPause(final Transition transition) { + } + + @Override + public void onTransitionResume(final Transition transition) { + } + }); + + TransitionManager.beginDelayedTransition(this, transitionSet); } animateButtons(false); animateBackground(false);