From 513c63e877320bca4860dadc88e3a14ffb861e36 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 24 Apr 2013 15:27:56 +0900 Subject: [PATCH] [PB8] Add an animation upon status change. Bug: 7600384 Change-Id: If5efb9357075193d10255187008e870e2933bdb8 --- .../dictionarypack/ButtonSwitcher.java | 28 +++++++++++++++---- .../DictionaryListInterfaceState.java | 6 ++++ .../dictionarypack/WordListPreference.java | 16 +++++++++-- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/java/src/com/android/inputmethod/dictionarypack/ButtonSwitcher.java b/java/src/com/android/inputmethod/dictionarypack/ButtonSwitcher.java index 196c2113d..391a15c97 100644 --- a/java/src/com/android/inputmethod/dictionarypack/ButtonSwitcher.java +++ b/java/src/com/android/inputmethod/dictionarypack/ButtonSwitcher.java @@ -16,9 +16,12 @@ package com.android.inputmethod.dictionarypack; +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; import android.content.Context; import android.util.AttributeSet; import android.view.View; +import android.view.ViewPropertyAnimator; import android.widget.Button; import android.widget.FrameLayout; @@ -115,22 +118,35 @@ public class ButtonSwitcher extends FrameLayout { } private void animateButtonPosition(final int oldStatus, final int newStatus) { - animateButton(getButton(oldStatus), ANIMATION_OUT); - animateButton(getButton(newStatus), ANIMATION_IN); + final View oldButton = getButton(oldStatus); + final View newButton = getButton(newStatus); + if (null != oldButton && null != newButton) { + // Transition between two buttons : animate out, then in + animateButton(oldButton, ANIMATION_OUT).setListener( + new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(final Animator animation) { + animateButton(newButton, ANIMATION_IN); + } + }); + } else if (null != oldButton) { + animateButton(oldButton, ANIMATION_OUT); + } else if (null != newButton) { + animateButton(newButton, ANIMATION_IN); + } } public void setInternalOnClickListener(final OnClickListener listener) { mOnClickListener = listener; } - private void animateButton(final View button, final int direction) { - if (null == button) return; + private ViewPropertyAnimator animateButton(final View button, final int direction) { final float outerX = getWidth(); final float innerX = button.getX() - button.getTranslationX(); if (ANIMATION_IN == direction) { - button.animate().translationX(0); + return button.animate().translationX(0); } else { - button.animate().translationX(outerX - innerX); + return button.animate().translationX(outerX - innerX); } } } diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionaryListInterfaceState.java b/java/src/com/android/inputmethod/dictionarypack/DictionaryListInterfaceState.java index 8975d69a8..de3711c27 100644 --- a/java/src/com/android/inputmethod/dictionarypack/DictionaryListInterfaceState.java +++ b/java/src/com/android/inputmethod/dictionarypack/DictionaryListInterfaceState.java @@ -44,6 +44,12 @@ public class DictionaryListInterfaceState { return state.mOpen; } + public int getStatus(final String wordlistId) { + final State state = mWordlistToState.get(wordlistId); + if (null == state) return MetadataDbHelper.STATUS_UNKNOWN; + return state.mStatus; + } + public void setOpen(final String wordlistId, final int status) { final State newState; final State state = mWordlistToState.get(wordlistId); diff --git a/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java b/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java index 2d15bed76..1cf9196b5 100644 --- a/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java +++ b/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java @@ -191,8 +191,20 @@ public final class WordListPreference extends Preference { ((ViewGroup)view).setLayoutTransition(null); final ButtonSwitcher buttonSwitcher = (ButtonSwitcher)view.findViewById(R.id.wordlist_button_switcher); - buttonSwitcher.setStatusAndUpdateVisuals(mInterfaceState.isOpen(mWordlistId) ? - getButtonSwitcherStatus(mStatus) : ButtonSwitcher.STATUS_NO_BUTTON); + if (mInterfaceState.isOpen(mWordlistId)) { + // The button is open. + final int previousStatus = mInterfaceState.getStatus(mWordlistId); + buttonSwitcher.setStatusAndUpdateVisuals(getButtonSwitcherStatus(previousStatus)); + if (previousStatus != mStatus) { + // We come here if the status has changed since last time. We need to animate + // the transition. + buttonSwitcher.setStatusAndUpdateVisuals(getButtonSwitcherStatus(mStatus)); + mInterfaceState.setOpen(mWordlistId, mStatus); + } + } else { + // The button is closed. + buttonSwitcher.setStatusAndUpdateVisuals(ButtonSwitcher.STATUS_NO_BUTTON); + } buttonSwitcher.setInternalOnClickListener(mActionButtonClickHandler); view.setOnClickListener(mPreferenceClickHandler); }