我正在 Android 应用程序上创建 UI。当我调用 view.startAnimation(anim) 时。它的工作原理是根据需要淡出按钮,但一旦动画完成从 1 -> 0 的 alpha 插值,它就会重新出现。我假设 alpha 重置为其原始值。我不确定我错过了什么。
我正在使用最新的 android api,并在 Galaxy s9 物理设备上进行测试。
我调查了以下页面来尝试解决我的问题,一个是 YouTube 教程,另一个是 Stack Overflow 回答的问题。 https://www.youtube.com/watch?v=x6QS6m1pYMs Fade In Fade Out Android Animation in Java
我已将所有内容设置如下, xml:
<ToggleButton
android:id="@+id/my_button_toggle"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_gravity="bottom|right"
android:gravity="center" />
<Button
android:id="@+id/my_button"
android:textSize="12dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_gravity="bottom|center"/>
上面是在 FrameLayout 内部定义的,前面定义了一个 fragment 。
set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:id="@+id/fade_out"
android:duration="500"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:fillAfter="true" />
</set>
调用 Java 像这样组合在一起..
//global
ToggleButton togglebutton;
Button button;
void hideUI(){
fade(togglebutton);
fade(button);
}
void fade(View view){
Button fadingButton = (Button) view;
Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out);
fadingButton.startAnimation(fadeOutAnimation);
}
最佳答案
尝试添加:
fadeOutAnimation.setFillAfter(true);
开始动画后。
像这样:
void fade(View view){
Button fadingButton = (Button) view;
Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out);
fadingButton.startAnimation(fadeOutAnimation);
fadeOutAnimation.setFillAfter(true);
}
编辑:
没关系,我看到你确实在 xml 中添加了它。
看这里:
关于java - 切换按钮元素消失但在动画完成后突然重新出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56214062/