How a Translate Animation works: Android

妖精的绣舞 提交于 2019-12-30 06:55:26

问题


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);


来源:https://stackoverflow.com/questions/16727647/how-a-translate-animation-works-android

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