我开发了一个基于ViewGroup
我的问题是我需要在动画结束后保存项目的位置。我打了电话setFillAfter(true)
在我创建的动画对象中AnimationListener
并在其中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);
}
}
请给我一个建议,我应该如何根据动画的结束位置重置视图的位置?
你必须使用ObjectAnimator
,它从 API 11 级别开始工作。它自动改变视图位置,
这是例子
ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(mContent_container, "translationX", startX, endX);
objectAnimator.setDuration(1000);
objectAnimator.start();
感谢 JUL 的帮助answer https://stackoverflow.com/questions/2650351/android-translateanimation-resets-after-animation/9837296#9837296
如果找不到您的应用程序object animator
,更改 API 级别Project -> properties -> Android
, than import android.animation.ObjectAnimator;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)