Home > front end >  Android scale and rotate animation not smooth, jumps
Android scale and rotate animation not smooth, jumps

Time:11-09

What is the difference between these two animation approaches? They both achieve the same result, but the first one is jerky while the second one is smooth. Why?

This animation progresses nicely until the very end when it suddenly jumps into its final position. Everything seems to be very straightforward, so whats up with this?

        val cube = homeBinding.imgCube
        val animSet = AnimationSet(true)

        val rotateAnimation = RotateAnimation(0.0f,
            720.0f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f)

        val scaleAnimation: Animation = ScaleAnimation(
            0.1f, 0.4f,
            0.2f, 0.4f,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f)

        scaleAnimation.interpolator = LinearInterpolator()
        rotateAnimation.interpolator = LinearInterpolator()


        animSet.duration = 3000
        animSet.addAnimation(rotateAnimation)
        animSet.addAnimation(scaleAnimation)

        cube.startAnimation(animSet)

and xml:

  <ImageView
            android:id="@ id/imgCube"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:contentDescription="some"
            android:scaleType="centerInside"
            android:scaleX="0.4"
            android:scaleY="0.4"
            app:srcCompat="@drawable/kk" />

enter image description here

While this one works just fine.

But what's the difference?

 cube.animate().scaleX(0.4f)
            .scaleY(0.4f)
            .setDuration(3000)
            .rotation(720f)
            .setInterpolator (LinearInterpolator())
            .start();

enter image description here

CodePudding user response:

The first approach is using a RotateAnimation and a ScaleAnimation with an AnimationSet, the second approach (which is by far better for this task, let alone the much shorter code) is using ViewPropertyAnimator.

All I can say I had buggy problems with AnimationSet and these kind of animations before, so I strongly recommend ViewPropertyAnimator for further Animations

I am guessing in the first approach it is jumping back because you did not specify the setFillAfter(true) method in your AnimationSet, and/or it has something to do with the Pivots (RELATIVE_TO_SELF)

  • Related