android can not use ViewPropertyAnimator's scale and translation in the same time

30 views Asked by At

I want to move one view over the other to make their left top corner aligned, and this is the views at the beginning of the animation: the views at the beginning of the animation

and I use this code to do the animation:

View view = findViewById(R.id.test_image);
View finalView = findViewById(R.id.final_wrapper);
float scale = new Random().nextFloat();            //this scale is only known at runtime 
view.animate().scaleX(scale).scaleY(scale)
      .translationX(finalWrapper.getLeft() - view.getLeft())
      .translationY(finalWrapper.getTop() - view.getTop())
      .setDuration(500).start();

this code works well if I only use scale or only use translation, but when I use them together, I can not move the red view right to the purple view's left top corner, there will always be some deviation:

deviation over the two views

So, how to move the red view over the purple view to make their left top corner aligned when using scale and translation in the meantime?

When I only use translation:

view.animate()
      .translationX(finalWrapper.getLeft() - view.getLeft())
      .translationY(finalWrapper.getTop() - view.getTop())
      .setDuration(500).start();

It works fine: enter image description here

1

There are 1 answers

0
snachmsm On BEST ANSWER

check out pivot setting before starting animation

view.setPivotX(0)
view.setPivotY(0)

by default pivot is set to center of view (50% of width and height), so scale animation is shrinking it "to center", thats why you got additional move distance

more info how to (pre-)set "animation center" point in HERE