我在 RelativeLayout
中添加了带有 ObjectAnimator
的 childView。现在,当我从 RelativeLayout
中删除所有子项时,我仍然会触发 onAnimationEnd
。
一旦我从 RelativeLayout 中删除了所有 subview ,我将如何停止所有 subview 的动画。
RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
RelativeLayout.LayoutParams layoutParams = getCircleLayoutParam(R.dimen.game_circle_width, R.dimen.game_circle_ht);
container.addView(view, layoutParams);
ObjectAnimator scale = ObjectAnimator.ofPropertyValuesHolder(view,
PropertyValuesHolder.ofFloat("alpha", 0f),
PropertyValuesHolder.ofFloat("scaleY", 1f),
PropertyValuesHolder.ofFloat("y", 0, mWindowHeight));
scale.setDuration(SPEED_TIME);
scale.start();
scale.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
//Getting triggered even after calling container.removeAllViews()
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (scale != null){
scale.removeAllListeners();
scale.cancel();
scale.end();
}
container.removeAllViews();
container.invalidate();
}
});
在单击按钮时调用 container.removeAllViews()
和 container.invalidate()
。我仍然会触发 onAnimationEnd()
。
非常感谢任何回复。
最佳答案
将View
添加到容器时,将Animator
作为标签传递:
view.setTag(scale);
OnClickListener
:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < container.getChildCount(); i++)
{
View v = container.getChildAt(i);
Object o = v.getTag();
if (o != null && o instanceof ObjectAnimator)
{
((ObjectAnimator)o).cancel();
// or ...end(), see below
}
}
container.removeAllViews();
container.invalidate();
}
});
使用 ValueAnimator.cancel()
或 ValueAnimator.end()
取决于您想要达到的结果,参见 documentation
如果该标签已用于其他目的,则可以保留一个包含 ObjectAnimator
的列表,并在 for 循环中遍历该列表。
关于Android Removing View 不结束它的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34893298/