确定默认的Android复选框动画何时结束

8

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

我想要在动画结束时进行识别。

根据文档,有两种可能的方式 -

  • 当复选框被选中或取消选中(setOnCheckedChangeListener()),获取当前的Animation对象(getAnimation())并在其上注册一个监听器(setAnimationListener())。不幸的是,这并不起作用 - 此时,Animation对象为空。

  • 子类化Checkbox对象并实现它的onAnimationEnd()方法。遗憾的是,这也不起作用 - 这个方法从来没有被调用过。

我错过了什么?有什么好方法可以识别这样的默认动画何时结束?我认为相关事件可以在活动中的其他视图上进行注册,但我无法弄清楚哪些视图。

这是第一种方法的相关代码片段(animation总是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);
       }
   });
1个回答

0
我在复选框上设置了以下 OnCheckedChangeListener,取得了一定的成功。
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    Drawable drawable = buttonView.getButtonDrawable().getCurrent();
    if (drawable instanceof AnimatedVectorDrawable) {
        AnimatedVectorDrawable animatedDrawable = (AnimatedVectorDrawable) drawable;
        animatedDrawable.registerAnimationCallback(new Animatable2.AnimationCallback() {
            @Override
            public void onAnimationEnd(Drawable drawable) {
                super.onAnimationEnd(drawable);
                // your action goes here
            }
        });
    } else {
        // and maybe here as well as a fallback
    }
}

很常见(也许有50%的时间)onAnimationEnd回调会被触发两次。虽然我不明白为什么。


onAnimationEnd回调被触发两次,我猜如果它是一个单选按钮,你会在第一个单选按钮取消选择后得到onCheckedChange回调,然后是第二个单选按钮的选择。 - kralvarado

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接