Android ScaleAnimation and TranslateAnimation how to avoid ScaleAnimation movement

I have an AnimationSet with inside ScaleAnimation and TranslateAnimation, like this:

TranslateAnimation:

TranslateAnimation goTopFromRight = new TranslateAnimation(0, -(right.getLeft()-top.getLeft()), 0,-(right.getTop()-top.getTop())); 

ScaleAnimation:

 ScaleAnimation = setSizeForTop = new ScaleAnimation(1, 2, 1, 2); 

and AnimationSet:

 bringToLeftFromTopAnimationSet = new AnimationSet(true); bringToTopFromRightAnimationSet.addAnimation(goTopFromRight); bringToTopFromRightAnimationSet.addAnimation(setSizeForTop); 

The problem is that when I try to use only ScaleAnimation, my element moves to the position I want, but I use ScaleAnimation with TranslateAnimation in AnimationSet, my element translates more than I need, as if ScaleAnimation introduces some additional movements and I I don’t know how to remove them.

Thank you for your help.

+6
source share
4 answers

To avoid moving ScaleAnimation when using ScaleAnimation and TranslateAnimation, you need to multiply TranslateAnimation parameters by ScaleAnimation parameters as follows:

 TranslateAnimation goRightFromTop; ScaleAnimation sizeNotUp; goRightFromTop = new TranslateAnimation(0,(-( invisibleCenterImageView.getLeft() - imageViewRight.getLeft() ))*(1/0.6667f), 0, (-( invisibleCenterImageView.getTop() - imageViewRight.getTop()))*(1/0.6667f)); sizeNotUp = new ScaleAnimation(1,0.6667f,1,0.6667f,Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5); 

And then, surprise that you removed the ScaleAnimation movement.

+4
source

The correct solution is to reorder the animation. Scale must be first:

 bringToTopFromRightAnimationSet.addAnimation(setSizeForTop); bringToTopFromRightAnimationSet.addAnimation(goTopFromRight); 
+5
source

I remember that there were strange problems with nested animations. Did you try to manually adjust the pivot point? See public ScaleAnimation (float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) from http://developer.android.com/reference/android/view/animation/ScaleAnimation.html . It might work.

+1
source

My solution is to add a container outside of your view and apply scale animation to your view by applying translation animations to the container.
Then you can make more complex animations even through XML ~

0
source

Source: https://habr.com/ru/post/910725/


All Articles