1

Here i am trying to move a view on a path with ObjectAnimator and also need to set one more scale animation on same view.

ObjectAnimator objectAnimator = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
        {
            objectAnimator = ObjectAnimator.ofFloat(view, View.X, View.Y, path);
        }
        if (objectAnimator != null) {
            objectAnimator.setDuration(2500);
            objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
            objectAnimator.start();
view.startAnimation(scaleRection);// this is not working because changing of x y position

need to start another Animation when objectAnimator.start();

also tried with listener

objectAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    view.startAnimation(scaleRection);
                }

                @Override
                public void onAnimationEnd(Animator animation)
                {

                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
Vora
  • 347
  • 2
  • 15

2 Answers2

4

You can also use an AnimatorSet play together https://developer.android.com/reference/android/animation/AnimatorSet.html#playTogether and it's builder function https://developer.android.com/reference/android/animation/AnimatorSet.Builder

To play Two ObjectAnimator together

e.g.

                AnimatorSet animationSet = new AnimatorSet();

                ObjectAnimator scaleY = ObjectAnimator.ofFloat(view,"scaleY", 1f, 0f);
                scaleY.setDuration(5000);
                scaleY.setInterpolator(new AccelerateDecelerateInterpolator());
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(view,"scaleX", 1f, 0f);
                scaleX.setDuration(5000);
                scaleX.setInterpolator(new AccelerateDecelerateInterpolator());
                animationSet.playTogether(scaleX, scaleY);
                animationSet.start();
Andrew
  • 8,198
  • 2
  • 15
  • 35
2

I would suggest using ViewPropertyAnimator.

From the docs :

This class enables automatic and optimized animation of select properties on View objects. If only one or two properties on a View object are being animated, then using an ObjectAnimator is fine; the property setters called by ObjectAnimator are well equipped to do the right thing to set the property and invalidate the view appropriately. But if several properties are animated simultaneously, or if you just want a more convenient syntax to animate a specific property, then ViewPropertyAnimator might be more well-suited to the task.

This class may provide better performance for several simultaneous animations, because it will optimize invalidate calls to take place only once for several properties instead of each animated property independently causing its own invalidation. Also, the syntax of using this class could be easier to use because the caller need only tell the View object which property to animate, and the value to animate either to or by, and this class handles the details of configuring the underlying Animator class and starting it.

You can chain as many animations as you like at once in one line of code :

view.animate().translationX(...).translationY(...).scaleX(...).scaleY(...).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(2500);

if you need different values for your duration or similar, you can do it with two lines :

view.animate().translationX().setDuration(...) ...    
view.animate().scaleX().setDuration(...) ...

There are also methods translationXBy() and scaleXBy() which might be more suitable for your case, and you can also set a listener etc. Check the docs for all available methods

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38