gpt4 book ai didi

android - 动画自定义对话框

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:21 24 4
gpt4 key购买 nike

我正在尝试让自定义对话框看起来好像从 TextView 中滑落一样。这可能吗?我似乎无法将任何动画应用于对话框类。我在构造函数中试过这一行,但没有效果:

this.getWindow().setWindowAnimations(R.anim.paranimation);

我什至不确定动画是否正确,但一旦我看到它在做什么,我就能调整它。为了完整起见,我将在下面列出。我不是在寻求有关实际动画的帮助,只是寻求对话框的应用程序。

paranimation.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-200%"
android:toXDelta="0%"
android:fromYDelta="200%"
android:toYDelta="0%"
android:duration="3000"
android:zAdjustment="top">
</translate>

最佳答案

我今天一直在努力处理对话框动画,终于使用样式让它工作了,所以这里有一个例子。

首先,最重要的事情——我今天可能让它以 5 种不同的方式工作,但无法分辨,因为……如果您的设备动画设置设置为“无动画”(设置 → 显示 → 动画),那么无论您做什么,对话框都不会动画化!

以下是我的 styles.xml 的精简版本。希望它是不言自明的。这应该位于 res/values 中。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PauseDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">@style/PauseDialogAnimation</item>
</style>

<style name="PauseDialogAnimation">
<item name="android:windowEnterAnimation">@anim/spin_in</item>
<item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
</style>
</resources>

windowEnterAnimation 是我的动画之一,位于 res\anim 中。windowExitAnimation 是 Android SDK 中的动画之一。

然后,当我在我的 Activity onCreateDialog(int id) 方法中创建对话框时,我执行以下操作。

Dialog dialog = new Dialog(this, R.style.PauseDialog);

// Setting the title and layout for the dialog
dialog.setTitle(R.string.pause_menu_label);
dialog.setContentView(R.layout.pause_menu);

或者,您可以通过以下方式设置动画,而不是使用采用主题的 Dialog 构造函数。

Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;

关于android - 动画自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5650713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com