How a Translate Animation works: Android

2019-02-14 14:10发布

问题:

I'm trying to move a RelativeLayout using TranslateAnimation. Code I have written for performing the same is:

translateAnimation = new TranslateAnimation(0, 0, heightOfRootView-excuseContainer.getHeight(), currentYPoint);
translateAnimation.setRepeatMode(0);
translateAnimation.setDuration(500);
translateAnimation.setFillAfter(true);
excuseContainer.startAnimation(translateAnimation);

I'm trying to start animation from current y position of particular view (I do not need to change x position of view) But animation is staring every time from its first y point. How can I perform this action from current y position of view to desired position of view.

Here heightOfRootView stands for height of full screen, excuseContainer is that view which I want to move with animation and currentYPoint is last y point of excuseContainer.

EDIT: I there any Translate animation tutorial available. I searched for it but I din't found..

Thanks for your support.

回答1:

The 3rd parameter of the TranslateAnimation constructor you are using is a delta value, so the starting point is calculated like:

currentYPos + startingDeltaY

Since you seem to be passing in a Y value that refers to the location of something on the screen, this delta value won't be correct.

Try using this constructor:

public TranslateAnimation (int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

Like this:

new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, heightOfRootView-excuseContainer.getHeight(), Animation.ABSOLUTE, currentYPoint);