识别默认的Android Checkbox动画何时结束

Hex*_*gon 5 checkbox animation android material-design

单击复选框时,将触发默认的Android材质设计动画(从空白到“ V”标记,从“ V”标记到空白)。

我想确定动画何时结束。

根据文档,这应该可以通过以下两种方式之一实现:

  • 选中或取消选中复选框(setOnCheckedChangeListener())时,获取当前的Animation对象(getAnimation()),并在其上注册一个侦听器(setAnimationListener())。不幸的是,这是行不通的-目前,Animation对象是null

  • 子类化Checkbox对象并实现其onAnimationEnd()方法。不幸的是,这还行不通-永远不会调用该方法。

我想念什么?确定这种默认动画何时结束的好方法是什么?我认为相关事件可以在活动中的其他视图上注册,但是我不知道是哪个。

这是第一种方法的相关代码段(动画始终为null)-

    CheckBox checkBox = (CheckBox)findViewById(R.id.checkbox1);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            Animation animation = compoundButton.getAnimation();
            Log.d("Checkbox", "Animation is " + animation);
       }
   });
Run Code Online (Sandbox Code Playgroud)

Mic*_*ung 0

我在复选框上设置以下 OnCheckedChangeListener 有点成功。

\n\n
@Override\npublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {\n    Drawable drawable = buttonView.getButtonDrawable().getCurrent();\n    if (drawable instanceof AnimatedVectorDrawable) {\n        AnimatedVectorDrawable animatedDrawable = (AnimatedVectorDrawable) drawable;\n        animatedDrawable.registerAnimationCallback(new Animatable2.AnimationCallback() {\n            @Override\n            public void onAnimationEnd(Drawable drawable) {\n                super.onAnimationEnd(drawable);\n                // your action goes here\n            }\n        });\n    } else {\n        // and maybe here as well as a fallback\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

通常(也许 50% 的时间)onAnimationEnd回调会被触发两次。不过,我不明白为什么。

\n