From 2ef4aeb4fd0934e4591a7a9d6298c64e997f0c05 Mon Sep 17 00:00:00 2001 From: Kirill Grouchnikov Date: Thu, 21 Sep 2017 20:46:33 -0400 Subject: [PATCH] Conditional use of workaround for 22-24 devices Bug: 32136707 Test: ./gradlew support-design:connectedCheck --info --daemon -Pandroid.testInstrumentationRunnerArguments.class=android.support.design.widget.FloatingActionButtonTest Change-Id: Icdf198e9923af4baa1456496efdfc25b20abe582 PiperOrigin-RevId: 169627986 --- .../FloatingActionButtonImplLollipop.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/src/android/support/design/widget/FloatingActionButtonImplLollipop.java b/lib/src/android/support/design/widget/FloatingActionButtonImplLollipop.java index 18c93297b..b28b6bdf5 100644 --- a/lib/src/android/support/design/widget/FloatingActionButtonImplLollipop.java +++ b/lib/src/android/support/design/widget/FloatingActionButtonImplLollipop.java @@ -16,6 +16,7 @@ package android.support.design.widget; +import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.StateListAnimator; @@ -31,6 +32,8 @@ import android.os.Build; import android.support.annotation.RequiresApi; import android.support.v4.graphics.drawable.DrawableCompat; import android.view.View; +import java.util.ArrayList; +import java.util.List; @RequiresApi(21) class FloatingActionButtonImplLollipop extends FloatingActionButtonImpl { @@ -119,15 +122,20 @@ class FloatingActionButtonImplLollipop extends FloatingActionButtonImpl { // Animate translationZ to 0 if not pressed set = new AnimatorSet(); - set.playSequentially( - ObjectAnimator.ofFloat(mView, "elevation", elevation).setDuration(0), - // This is a no-op animation which exists here only for introducing the duration - // because setting the delay (on the next animation) via "setDelay" or "after" - // can trigger a NPE between android versions 22 and 24 (due to a framework - // bug). The issue has been fixed in version 25. - ObjectAnimator.ofFloat(mView, View.TRANSLATION_Z, mView.getTranslationZ()) - .setDuration(PRESSED_ANIM_DELAY), + List animators = new ArrayList<>(); + animators.add(ObjectAnimator.ofFloat(mView, "elevation", elevation).setDuration(0)); + if (Build.VERSION.SDK_INT >= 22 && Build.VERSION.SDK_INT <= 24) { + // This is a no-op animation which exists here only for introducing the duration + // because setting the delay (on the next animation) via "setDelay" or "after" + // can trigger a NPE between android versions 22 and 24 (due to a framework + // bug). The issue has been fixed in version 25. + animators.add( + ObjectAnimator.ofFloat(mView, View.TRANSLATION_Z, mView.getTranslationZ()) + .setDuration(PRESSED_ANIM_DELAY)); + } + animators.add( ObjectAnimator.ofFloat(mView, View.TRANSLATION_Z, 0f).setDuration(PRESSED_ANIM_DURATION)); + set.playSequentially(animators.toArray(new Animator[0])); set.setInterpolator(ANIM_INTERPOLATOR); stateListAnimator.addState(ENABLED_STATE_SET, set);