我正在创建动画来显示和隐藏我的 apk 的侧边栏。但是当事件发生时,两个动画都会被执行。
我的切换动画代码是
private boolean showingBar = true;
private void switchSideBar() {
if (showingBar) {
sideBar.startAnimation(hideBar);
Toast.makeText(this, "Showing", Toast.LENGTH_SHORT).show();
showingBar = false;
} else {
showingBar = true;
Toast.makeText(this, "Hiding", Toast.LENGTH_SHORT).show();
sideBar.startAnimation(showBar);
}
}
我添加了这些 Toast 以查看发生了什么,当事件发生时只显示一个文本。
动画的代码是这样的
显示栏
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-50" android:toXDelta="0"
android:duration="300" />
隐藏栏
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="-50"
android:duration="300" />
发生了什么事?
最佳答案
为什么说两个动画都执行了呢?从基于 toast 的分析看来,似乎只有一个动画被执行。
如果你说两个动画都被执行是因为元素在动画结束时返回到原始位置,那可能是因为你没有设置 fillAfter
to true
。在你的动画中。您还可以查看 onAnimationEnd
作为在打开和关闭时更改侧边栏位置的选项(从而使其永久化)。
关于android - 在一个 View 上隐藏/显示动画,当同时发生时 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4889961/