2

我正在创建一个自定义切换开关,我大部分时间都完成了设计和功能,但有一件事阻碍了我,我试图在切换到拇指时添加动画,基本上是一个按钮,向右的动画效果很好,但问题在于向左的动画是我用来播放这两个动画的代码。

    private void playToggleanimation()
    {
        if(toggleAnimation != null && !toggleAnimation.hasEnded())
        {
            toggleAnimation.setAnimationListener(null);
            toggleAnimation.cancel();
        }
        View v = findViewById(button);
        toggleAnimation = (checked) ? new TranslateAnimation(v.getLeft(), getMeasuredWidth() / 2, 0, 0) : new TranslateAnimation(v.getLeft(), 0, 0, 0);
        toggleAnimation.setAnimationListener(listener);
        toggleAnimation.setFillAfter(true);
        toggleAnimation.setFillEnabled(true);
        toggleAnimation.setDuration(250);
        v.clearAnimation();
        v.startAnimation(toggleAnimation);
    }

谢谢你的帮助。

4

1 回答 1

2

我发现问题出在getLeft()方法上,而不是使用这条线

toggleAnimation = (checked) ? new TranslateAnimation(v.getLeft(), getMeasuredWidth() / 2, 0, 0) : new TranslateAnimation(v.getLeft(), 0, 0, 0);

我使用下面的代码行来创建动画,效果很好。

toggleAnimation = (checked) ? new TranslateAnimation(v.getLeft(), getMeasuredWidth() / 2, 0, 0) : new TranslateAnimation(getMeasuredWidth() / 2, 0, 0, 0);

getLeft()方法总是返回 0。

于 2012-04-12T09:24:21.567 回答