ObjectAnimator的一部分不与API级别23下面的AnimationSet playTogether()同时播放

问题描述 投票:2回答:1

当我在API级别23上使用ObjectAnimator设置多个playTgether()时,不会同时播放动画,但它的工作方式与我预期的API级别24相同。

然而,当我用ObjectAnimator设置每个play()时,动画的工作方式正如我在仿真器API 23和24上所预期的那样。

有人可以告诉我问题的原因吗? 我将附上GIF和我的代码。

The animations

在API级别23模拟器上 api_23.gif: api_23.gif

在API级别24模拟器上 api_24.gif: api_24.gif

Code

activity_text.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presentation.view.activity.TestActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/browser_actions_title_color" />
</RelativeLayout>
TestActivity.kt
class TestActivity: AppCompatActivity() {

    private val TRANSLATION_X_START = 0f
    private val TRANSLATION_X_END = 500f
    private val TRANSLATION_Y_START = 0f
    private val TRANSLATION_Y_END = 500f
    private val DELAY_TRANSLATION = 2000
    private val TRANSLATE_ANIMATION_TIME = 800

    private val DEFAULT_SCALE = 1f
    private val MAX_SCALE = 3f
    private val DELAY_SCALE = 2000
    private val SCALE_ANIMATION_TIME = 800

    private val MIN_ALPHA = 0f
    private val MAX_ALPHA = 255f
    private val DELAY_ALPHA = 2250
    private val ALPHA_ANIMATION_TIME = 800

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        startAnimation(imageView)
    }

    private fun startAnimation(view: View) {

        val animationSet  = AnimatorSet()

        val animatorX = ObjectAnimator.ofFloat(view, "x", TRANSLATION_X_START, TRANSLATION_X_END)
        animatorX.interpolator = AccelerateDecelerateInterpolator()
        animatorX.duration = TRANSLATE_ANIMATION_TIME.toLong()
        animatorX.startDelay = DELAY_TRANSLATION.toLong()

        val animatorY = ObjectAnimator.ofFloat(view, "y", TRANSLATION_Y_START, TRANSLATION_Y_END)
        animatorY.interpolator = AccelerateDecelerateInterpolator()
        animatorY.duration = TRANSLATE_ANIMATION_TIME.toLong()
        animatorY.startDelay = DELAY_TRANSLATION.toLong()

        val animatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", DEFAULT_SCALE, MAX_SCALE)
        animatorScaleX.interpolator = LinearInterpolator()
        animatorScaleX.duration = SCALE_ANIMATION_TIME.toLong()
        animatorScaleX.startDelay = DELAY_SCALE.toLong()

        val animatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", DEFAULT_SCALE, MAX_SCALE)
        animatorScaleY.interpolator = LinearInterpolator()
        animatorScaleY.duration = SCALE_ANIMATION_TIME.toLong()
        animatorScaleY.startDelay = DELAY_SCALE.toLong()

        val animatorAlpha = ObjectAnimator.ofFloat(view, "alpha", MAX_ALPHA, MIN_ALPHA)
        animatorAlpha.interpolator = LinearInterpolator()
        animatorAlpha.duration = ALPHA_ANIMATION_TIME.toLong()
        animatorAlpha.startDelay = DELAY_ALPHA.toLong()

        animationSet.playTogether(animatorX, animatorScaleX, animatorY, animatorScaleY, animatorAlpha)

        animationSet.addListener(object : Animator.AnimatorListener {
            override fun onAnimationEnd(animation: Animator) {
            }
            override fun onAnimationStart(animation: Animator) {
            }
            override fun onAnimationCancel(animation: Animator) {}

            override fun onAnimationRepeat(animation: Animator) {}
        })

        animationSet.start()
    }
}

Code works well for both api23 and api24

animationSet.play(animatorX)
animationSet.play(animatorScaleX)
animationSet.play(animatorY)
animationSet.play(animatorScaleY)
animationSet.play(animatorAlpha)
android kotlin android-animation objectanimator
1个回答
0
投票

我遇到了同样的问题,显然,它是SDK上的一个BUG。我的解决方法是仅在较高的API级别上使用动画设置,并在降低时处理单独的控制

if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.M) {
    // Bug in AnimatorSet for API 23
    animation1.start()
    animation2.start()
} else {
    val animation = AnimatorSet()
    animation.playTogether(animation1, animation2)
    animation.start()
}
© www.soinside.com 2019 - 2024. All rights reserved.