Android ScaleAnimation and TranslateAnimation, how to avoid ScaleAnimation movement

泄露秘密 提交于 2020-01-22 12:31:26

问题


I have an AnimationSet with inside a ScaleAnimation and a 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 the ScaleAnimation, my item goes to the position i want, but whe I am using the ScaleAnimation with the TranslateAnimation in the AnimationSet, my item translates more than i need, as if ScaleAnimation introduces some supplementary movements abd I don't know how to delete them.

Thank you for your help.


回答1:


The correct solution is to change order of animations. Scale must go first:

bringToTopFromRightAnimationSet.addAnimation(setSizeForTop);
bringToTopFromRightAnimationSet.addAnimation(goTopFromRight);



回答2:


To avoid the ScaleAnimation movement when using the ScaleAnimation and TranslateAnimation, you have to multiply the TranslateAnimation parametres with those from ScaleAnimation like this:

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 you deleted the ScaleAnimation movement.




回答3:


I remember having weird problems with nested animations as well. Have you tried setting the pivot point manually? 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 for it. It could work.




回答4:


My solution is to add a container outside your view, and apply the scale animation to your view while applying translate animation to the container.
Then you can make more complex animation even via XML~



来源:https://stackoverflow.com/questions/9705452/android-scaleanimation-and-translateanimation-how-to-avoid-scaleanimation-movem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!