动画自定义对话框 [英] Animate a custom Dialog
问题描述
我试图让自定义对话框看起来好像是从文本视图中滑下来的.这可能吗?我似乎无法将任何动画应用于对话框类.我在构造函数中试过这一行,但没有效果:
I'm trying to have a custom dialog appear as though it's sliding down from a text view. Is this possible? I can't seem to apply any animation to dialog class. I've tried this line in the constructor, but it has no effect:
this.getWindow().setWindowAnimations(R.anim.paranimation);
this.getWindow().setWindowAnimations(R.anim.paranimation);
我什至不确定动画是否正确,但是一旦我看到它在做什么,我就可以调整它.为了完整起见,我将在下面列出它.我不是在寻求有关实际动画的帮助,只是寻求对话框的应用程序.
I'm not even sure if the animation is correct, but I will be able adjust it once I see what it's doing. I'll list it below for the sake of completeness. I'm not looking for help on the actual animation, just the application to the dialog.
参数.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>
推荐答案
今天我一直在为 Dialog 动画苦苦挣扎,终于使用样式让它工作了,所以这里有一个例子.
I've been struggling with Dialog animation today, finally got it working using styles, so here is an example.
首先,最重要的事情 — 我今天可能让它以 5 种不同的方式工作,但无法确定,因为...如果您的设备动画设置设置为无动画"(设置 → 显示 → 动画)然后无论你做什么,对话框都不会被动画化!
To start with, the most important thing — I probably had it working 5 different ways today but couldn't tell because... If your devices animation settings are set to "No Animations" (Settings → Display → Animation) then the dialogs won't be animated no matter what you do!
以下是我的styles.xml 的精简版.希望它是不言自明的.这应该位于 res/values
中.
The following is a stripped down version of my styles.xml. Hopefully it is self-explanatory. This should be located in 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 中的动画之一.
The windowEnterAnimation
is one of my animations and is located in res\anim
.
The windowExitAnimation
is one of the animations that is part of the Android SDK.
然后当我在我的活动 onCreateDialog(int id)
方法中创建对话框时,我执行以下操作.
Then when I create the Dialog in my activities onCreateDialog(int id)
method I do the following.
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 构造函数.
Alternatively you could set the animations the following way instead of using the Dialog constructor that takes a theme.
Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
这篇关于动画自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!