Merge "Fix the translation not work if the animation is off" into sc-v2-dev

This commit is contained in:
Joanne Chung
2022-03-24 14:53:55 +00:00
committed by Android (Google) Code Review
2 changed files with 16 additions and 10 deletions

View File

@@ -101,26 +101,26 @@ public final class UiTranslationManager {
public static final String LOG_TAG = "UiTranslation"; public static final String LOG_TAG = "UiTranslation";
/** /**
* The state caller request to disable utranslation,, it is no longer need to ui translation. * The state the caller requests to enable UI translation.
* *
* @hide * @hide
*/ */
public static final int STATE_UI_TRANSLATION_STARTED = 0; public static final int STATE_UI_TRANSLATION_STARTED = 0;
/** /**
* The state caller request to pause ui translation, it will switch back to the original text. * The state caller requests to pause UI translation. It will switch back to the original text.
* *
* @hide * @hide
*/ */
public static final int STATE_UI_TRANSLATION_PAUSED = 1; public static final int STATE_UI_TRANSLATION_PAUSED = 1;
/** /**
* The state caller request to resume the paused ui translation, it will show the translated * The state caller requests to resume the paused UI translation. It will show the translated
* text again if the text had been translated. * text again if the text had been translated.
* *
* @hide * @hide
*/ */
public static final int STATE_UI_TRANSLATION_RESUMED = 2; public static final int STATE_UI_TRANSLATION_RESUMED = 2;
/** /**
* The state the caller request to enable ui translation. * The state the caller requests to disable UI translation when it no longer needs translation.
* *
* @hide * @hide
*/ */

View File

@@ -89,7 +89,7 @@ public class TextViewTranslationCallback implements ViewTranslationCallback {
originalTranslationMethod); originalTranslationMethod);
} }
final TransformationMethod transformation = mTranslationTransformation; final TransformationMethod transformation = mTranslationTransformation;
runWithAnimation( runChangeTextWithAnimationIfNeeded(
(TextView) view, (TextView) view,
() -> { () -> {
mIsShowingTranslation = true; mIsShowingTranslation = true;
@@ -122,7 +122,7 @@ public class TextViewTranslationCallback implements ViewTranslationCallback {
if (mTranslationTransformation != null) { if (mTranslationTransformation != null) {
final TransformationMethod transformation = final TransformationMethod transformation =
mTranslationTransformation.getOriginalTransformationMethod(); mTranslationTransformation.getOriginalTransformationMethod();
runWithAnimation( runChangeTextWithAnimationIfNeeded(
(TextView) view, (TextView) view,
() -> { () -> {
mIsShowingTranslation = false; mIsShowingTranslation = false;
@@ -232,10 +232,16 @@ public class TextViewTranslationCallback implements ViewTranslationCallback {
* Applies a simple text alpha animation when toggling between original and translated text. The * Applies a simple text alpha animation when toggling between original and translated text. The
* text is fully faded out, then swapped to the new text, then the fading is reversed. * text is fully faded out, then swapped to the new text, then the fading is reversed.
* *
* @param runnable the operation to run on the view after the text is faded out, to change to * @param changeTextRunnable the operation to run on the view after the text is faded out, to
* displaying the original or translated text. * change to displaying the original or translated text.
*/ */
private void runWithAnimation(TextView view, Runnable runnable) { private void runChangeTextWithAnimationIfNeeded(TextView view, Runnable changeTextRunnable) {
boolean areAnimatorsEnabled = ValueAnimator.areAnimatorsEnabled();
if (!areAnimatorsEnabled) {
// The animation is disabled, just change display text
changeTextRunnable.run();
return;
}
if (mAnimator != null) { if (mAnimator != null) {
mAnimator.end(); mAnimator.end();
// Note: mAnimator is now null; do not use again here. // Note: mAnimator is now null; do not use again here.
@@ -269,7 +275,7 @@ public class TextViewTranslationCallback implements ViewTranslationCallback {
@Override @Override
public void onAnimationRepeat(Animator animation) { public void onAnimationRepeat(Animator animation) {
runnable.run(); changeTextRunnable.run();
} }
}); });
mAnimator.start(); mAnimator.start();