动画结束后更改视图位置 [英] Change position of view after end of animation
问题描述
我开发基于的ViewGroup
窗口小部件,我的问题是,我需要保存动画结束后项目的位置。我叫 setFillAfter(真)
在我创建的动画对象 AnimationListener
,并在它的 onAnimationEnd
方法调用 View.layout(L,T,R,B)
来设置动画后的位置,因为我想动画新项目的位置开始下次。但在这种情况下,它看起来像数据项都layouted两次。如果我不使用 View.layout(L,T,R,B)
在动画结束,下一个动画开始于previous位置。这是我的code:
I develop a widget based on ViewGroup
and my problem is that I need to save position of items after the end of animation. I called setFillAfter(true)
in my animation object I created AnimationListener
and in it's onAnimationEnd
method call View.layout(l,t,r,b)
to set the position after animation, because I want animation to start from new item's position next time. But in this case it looks like items are layouted twice. If I don't use View.layout(l,t,r,b)
at the end of animation, next animation starts from previous position. Here is my code:
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);
}
}
请给我一个建议,我应该如何相应地重新设置视图的位置,以动画的结束位置?
Please give me an advice how should I reset view's position accordingly to the end position of animation?
推荐答案
我做到了。这里是code:
I did it. Here is the code:
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);
}
}
只是删除 setFillAfter(真)
写 setFillEnabled(真)
代替。但在这种情况下,我不明白 setFillEnabled()
工作的逻辑,因为它提供的行为不象描述文件。
Just removed setFillAfter(true)
and write setFillEnabled(true)
instead of. But in this case I don't understand the logic of setFillEnabled()
working, because it provides behavior not like describes in documentation.
这篇关于动画结束后更改视图位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!