From 26e1cf31315a7e747693784ca53eeca861e94aa7 Mon Sep 17 00:00:00 2001 From: dewarder Date: Wed, 15 Mar 2017 01:37:59 +0200 Subject: [PATCH] Update sample app --- .../holdingbuttonsample/MainActivity.java | 89 +++++++++++++++---- .../src/main/res/layout/activity_main.xml | 10 +++ 2 files changed, 82 insertions(+), 17 deletions(-) diff --git a/holdingbuttonsample/src/main/java/com/dewarder/holdingbuttonsample/MainActivity.java b/holdingbuttonsample/src/main/java/com/dewarder/holdingbuttonsample/MainActivity.java index d6877f9..66e0380 100644 --- a/holdingbuttonsample/src/main/java/com/dewarder/holdingbuttonsample/MainActivity.java +++ b/holdingbuttonsample/src/main/java/com/dewarder/holdingbuttonsample/MainActivity.java @@ -23,23 +23,34 @@ import android.view.View; import android.view.ViewPropertyAnimator; import android.widget.EditText; +import android.widget.TextView; import android.widget.Toast; import com.dewarder.holdinglibrary.HoldingButtonLayout; import com.dewarder.holdinglibrary.HoldingButtonLayoutListener; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + public class MainActivity extends AppCompatActivity implements HoldingButtonLayoutListener { private static final float SLIDE_TO_CANCEL_ALPHA_MULTIPLIER = 2.5f; private HoldingButtonLayout mHoldingButtonLayout; + private TextView mTime; private EditText mInput; private View mSlideToCancel; private int mAnimationDuration; + private ViewPropertyAnimator mTimeAnimator; private ViewPropertyAnimator mSlideToCancelAnimator; private ViewPropertyAnimator mInputAnimator; + private DateFormat mFormatter = new SimpleDateFormat("mm:ss:SS"); + private long mStartTime; + private Runnable mTimerRunnable; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -48,6 +59,7 @@ protected void onCreate(Bundle savedInstanceState) { mHoldingButtonLayout = (HoldingButtonLayout) findViewById(R.id.input_holder); mHoldingButtonLayout.addListener(this); + mTime = (TextView) findViewById(R.id.time); mInput = (EditText) findViewById(R.id.input); mSlideToCancel = findViewById(R.id.slide_to_cancel); @@ -56,13 +68,7 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void onBeforeExpand() { - if (mInputAnimator != null) { - mInputAnimator.cancel(); - } - - if (mSlideToCancelAnimator != null) { - mSlideToCancelAnimator.cancel(); - } + cancelAllAnimations(); mSlideToCancel.setTranslationX(0f); mSlideToCancel.setAlpha(0f); @@ -79,22 +85,24 @@ public void onAnimationEnd(Animator animation) { } }); mInputAnimator.start(); + + mTime.setTranslationY(mTime.getHeight()); + mTime.setAlpha(0f); + mTime.setVisibility(View.VISIBLE); + mTimeAnimator = mTime.animate().translationY(0f).alpha(1f).setDuration(mAnimationDuration); + mTimeAnimator.start(); + } @Override public void onExpand() { - + mStartTime = System.currentTimeMillis(); + invalidateTimer(); } @Override public void onBeforeCollapse() { - if (mInputAnimator != null) { - mInputAnimator.cancel(); - } - - if (mSlideToCancelAnimator != null) { - mSlideToCancelAnimator.cancel(); - } + cancelAllAnimations(); mSlideToCancelAnimator = mSlideToCancel.animate().alpha(0f).setDuration(mAnimationDuration); mSlideToCancelAnimator.setListener(new AnimatorListenerAdapter() { @@ -110,14 +118,25 @@ public void onAnimationEnd(Animator animation) { mInput.setVisibility(View.VISIBLE); mInputAnimator = mInput.animate().alpha(1f).setDuration(mAnimationDuration); mInputAnimator.start(); + + mTimeAnimator = mTime.animate().translationY(mTime.getHeight()).alpha(0f).setDuration(mAnimationDuration); + mTimeAnimator.setListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + mTime.setVisibility(View.INVISIBLE); + mTimeAnimator.setListener(null); + } + }); + mTimeAnimator.start(); } @Override public void onCollapse(boolean isCancel) { + stopTimer(); if (isCancel) { - Toast.makeText(this, "Action canceled!", Toast.LENGTH_LONG).show(); + Toast.makeText(this, "Action canceled! Time " + getFormattedTime(), Toast.LENGTH_SHORT).show(); } else { - Toast.makeText(this, "Action submited!", Toast.LENGTH_LONG).show(); + Toast.makeText(this, "Action submitted! Time " + getFormattedTime(), Toast.LENGTH_SHORT).show(); } } @@ -126,4 +145,40 @@ public void onOffsetChanged(float offset, boolean isCancel) { mSlideToCancel.setTranslationX(-mHoldingButtonLayout.getWidth() * offset); mSlideToCancel.setAlpha(1 - SLIDE_TO_CANCEL_ALPHA_MULTIPLIER * offset); } + + private void invalidateTimer() { + mTimerRunnable = new Runnable() { + @Override + public void run() { + mTime.setText(getFormattedTime()); + invalidateTimer(); + } + }; + + mTime.postDelayed(mTimerRunnable, 50); + } + + private void stopTimer() { + if (mTimerRunnable != null) { + mTime.getHandler().removeCallbacks(mTimerRunnable); + } + } + + private void cancelAllAnimations() { + if (mInputAnimator != null) { + mInputAnimator.cancel(); + } + + if (mSlideToCancelAnimator != null) { + mSlideToCancelAnimator.cancel(); + } + + if (mTimeAnimator != null) { + mTimeAnimator.cancel(); + } + } + + private String getFormattedTime() { + return mFormatter.format(new Date(System.currentTimeMillis() - mStartTime)); + } } diff --git a/holdingbuttonsample/src/main/res/layout/activity_main.xml b/holdingbuttonsample/src/main/res/layout/activity_main.xml index 53368cd..01bf6eb 100644 --- a/holdingbuttonsample/src/main/res/layout/activity_main.xml +++ b/holdingbuttonsample/src/main/res/layout/activity_main.xml @@ -6,6 +6,16 @@ android:layout_height="match_parent" tools:context="com.dewarder.holdingbuttonsample.MainActivity"> + +