我开发了一个基于 ViewGroup
的小部件,我的问题是我需要在动画结束后保存项目的位置。我在创建 AnimationListener
的动画对象中调用了 setFillAfter(true)
并在其中调用了 onAnimationEnd
方法 View.layout(l,t ,r,b)
来设置动画后的位置,因为我希望下次动画从新项目的位置开始。但在这种情况下,项目看起来像是布局了两次。如果我在动画结束时不使用 View.layout(l,t,r,b)
,下一个动画将从上一个位置开始。这是我的代码:
private void scrollUp() {
for(int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
final int index = i;
final int newleft = child.getLeft() + mOffsetBetweenItems;
final int newTop = child.getTop() - mOffsetBetweenItems;
TranslateAnimation scrollUp = new TranslateAnimation(0, mOffsetBetweenItems, 0, -mOffsetBetweenItems);
scrollUp.setDuration(1500);
scrollUp.setFillAfter(true);
scrollUp.setAnimationListener(
new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
child.layout(newleft, newTop, newleft + child.getMeasuredWidth(), newTop + child.getMeasuredHeight() );
}
}
);
child.startAnimation(scrollUp);
}
}
请给我一个建议,我应该如何根据动画的结束位置重置 View 的位置?
最佳答案
您必须使用 ObjectAnimator
,它从 API 11 级别开始工作。它会自动更改 View 位置,
这是例子
ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(mContent_container, "translationX", startX, endX);
objectAnimator.setDuration(1000);
objectAnimator.start();
感谢 JUL 的 answer
如果您的应用未找到 object animator
,请将 API 级别从 Project -> properties -> Android
更改为 import android.animation.ObjectAnimator;
关于android - 动画结束后更改 View 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8489830/