我正在创建一个自定义切换开关,我大部分时间都完成了设计和功能,但有一件事阻碍了我,我尝试在拇指上添加动画,基本上是一个按钮,向右的动画效果很完美,但问题是动画向左,这是我用来播放两个动画的代码。
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);
}
感谢您的帮助。
最佳答案
我发现问题出在 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。
关于android - Android TranslateAnimation 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10118210/