gpt4 book ai didi

android - 无法删除 ViewPropertyAnimator 的监听器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:42 26 4
gpt4 key购买 nike

当我在我的自定义 View 中运行此代码时,onAnimationStartonAnimationEnd 一直被重复调用。这不是很奇怪吗?作为一名 Android 程序员,我希望它们分别只被调用一次。

    final ViewPropertyAnimator animator = animate().setDuration(1000).alpha(0.0f);
animator.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
Utils.log("----------------start");
}

@Override
public void onAnimationEnd(Animator animation) {
Utils.log("--------- end");
}
}).start();

但是当 ViewPropertyAnimatorsetListener(null) 调用 onAnimationEnd 时,我试图通过移除监听器来解决这个问题。但不管文档中写了什么,它都没有用:

public ViewPropertyAnimator setListener (Animator.AnimatorListener listener)

Added in API level 12
Sets a listener for events in the underlying Animators that run the property animations.

Parameters
listener The listener to be called with AnimatorListener events. A value of null removes any existing listener.
Returns
This object, allowing calls to methods in this class to be chained.

有没有其他人遇到过这个奇怪的问题?也许这是 Android 的错误?

最佳答案

我刚遇到这个问题,但没有自定义 View 。

在我的例子中,我在同一个 View 上有两个动画。一个显示和隐藏。

原来如此

showView(){
myView.animate().translationY(myView.getHeight()).setListener(new ...{
...
onAnimationEnd(Animation animation){
hideView();
}
...}).start();
}
hideView(){
myView.animate().translationY(0).start();
}

当 hideView() 完成时,它会再次调用自己。这是因为旧的监听器仍然存在。修复它的关键最终是在第二个动画中将监听器设置为 null。例如

hideView(){
myView.animate().translationY(0).setListener(null).start();
}

关于android - 无法删除 ViewPropertyAnimator 的监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29407214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com