Android 在动画结束后更改视图的位置
我基于Android 在动画结束后更改视图的位置,android,android-animation,Android,Android Animation,我基于ViewGroup开发了一个小部件,我的问题是我需要在动画结束后保存项目的位置。我在我创建的动画对象中调用了setFillAfter(true),并在它的onAnimationEnd方法调用View.layout(l,t,r,b)来设置动画后的位置,因为我希望下次动画从新项目的位置开始。但在本例中,项目似乎被布置了两次。如果在动画结束时不使用View.layout(l,t,r,b),则下一个动画从上一个位置开始。这是我的密码: private void scrollUp() { f
ViewGroup
开发了一个小部件,我的问题是我需要在动画结束后保存项目的位置。我在我创建的动画对象中调用了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);
}
}
private void scrollUp(){
对于(int i=0;i
请告诉我如何根据动画的结束位置重置视图的位置?反转动画;首先给视图一个新位置,然后从旧位置到新位置设置动画。我做到了。代码如下:
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.setFillEnabled(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);
}
}
private void scrollUp(){
对于(int i=0;i
只需删除
setFillAfter(true)
并写入setFillEnabled(true)
而不是。但是在本例中,我不理解setFillEnabled()
工作的逻辑,因为它提供的行为与文档中描述的不同 必须使用ObjectAnimator
,它可以从API 11级别工作。它会自动改变视图位置
这是一个例子
ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(mContent_container, "translationX", startX, endX);
objectAnimator.setDuration(1000);
objectAnimator.start();
谢谢朱尔的帮助
如果您的应用程序未找到
ObjectAnimator
,请将API级别从Project->properties->Android
更改为import Android.animation.ObjectAnimator代码>更改位置意味着您希望在布局中设置底部或顶部,您希望这样..不是底部或顶部,而是具体的X、Y坐标,这些坐标由视图定义。布局()
方法如果我首先给出一个新位置,它将立即在新位置重新绘制我的视图,而不是调用startAnimation()一旦您更改了位置。如果我在View.startAnimation()
之前调用View.layout()
,它将重新绘制我的视图,动画将从新位置开始。但是除了调用View.layout()
之外,还有其他方法可以重新定位视图。就我个人而言,我倾向于修改视图拥有的LayoutParams。LayoutParams
只允许您指出新的宽度和高度,但我还需要指出左坐标和上坐标在AnimationEnd中设置新的左坐标修复了我的问题。谢谢在您的代码中,mOffsetBetweenItems将在什么时候设置/更新?注意:至少对棒棒糖不起作用(猜测在某些API级别上它是按照上面的方式实现的,但至少对棒棒糖不起作用),它会设置视图的动画。视图坐标不会随“对象animator”更改。