【问题标题】:Identify when default Android Checkbox animation ends识别默认的 Android Checkbox 动画何时结束
【发布时间】:2015-12-24 00:43:21
【问题描述】:

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

我想确定动画何时结束。

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

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

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

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

这是第一种方法的相关代码sn-p(动画总是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);
       }
   });

【问题讨论】:

    标签: android animation checkbox material-design


    【解决方案1】:

    我在 Checkbox 上设置了以下 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 取消选中第一个单选按钮,然后检查第二个单选按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多