为自定义对话框设置动画
我试图让自定义对话框看起来就像从文本视图中滑落一样。这可能吗?我似乎无法将任何动画应用于对话框类。我已经在构造函数中尝试过这一行,但没有效果:
this.getWindow().setWindowAnimations(R.anim.paranimation);
我什至不确定动画是否正确,但一旦我看到它在做什么,我就可以调整它。为了完整起见,我将在下面列出它。我不是在寻求有关实际动画的帮助,只是寻求对话框的应用程序。
paraanimation.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>
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);
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.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我今天一直在努力处理对话框动画,终于使用样式让它工作了,所以这里是一个例子。
首先,最重要的事情 — 我今天可能让它以 5 种不同的方式工作,但无法分辨,因为...如果您的设备动画设置设置为“无动画”(设置 → 显示 → 动画),那么对话框获胜无论你做什么都不要生气!
以下是我的 styles.xml 的精简版本。希望它是不言自明的。它应该位于
res/values
中。windowEnterAnimation
是我的动画之一,位于res\anim
中。windowExitAnimation
是 Android SDK 中的动画之一。然后,当我在活动
onCreateDialog(int id)
方法中创建对话框时,我执行以下操作。或者,您可以按以下方式设置动画,而不是使用采用主题的 Dialog 构造函数。
I've been struggling with Dialog animation today, finally got it working using styles, so here is an example.
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!
The following is a stripped down version of my styles.xml. Hopefully it is self-explanatory. This should be located in
res/values
.The
windowEnterAnimation
is one of my animations and is located inres\anim
.The
windowExitAnimation
is one of the animations that is part of the Android SDK.Then when I create the Dialog in my activities
onCreateDialog(int id)
method I do the following.Alternatively you could set the animations the following way instead of using the Dialog constructor that takes a theme.
我使用 ChrisJD 代码为对话框创建了淡入和淡出动画。
res/style.xml 内部
anim/fadein.xml 内部
anim/fadeout.xml 内部
MainActivity
I have created the Fade in and Fade Out animation for Dialogbox using ChrisJD code.
Inside res/style.xml
Inside anim/fadein.xml
Inside anim/fadeout.xml
MainActivity
从右到左(进入动画)和从左到右(退出动画):
styles.xml:
在 res/anim/ 中创建两个文件:
translate_right_side.xml:
translate_left_side。 xml:
在您的片段/活动中:
For right to left (entry animation) and left to right (exit animation):
styles.xml:
Create two files in res/anim/:
translate_right_side.xml:
translate_left_side.xml:
In you Fragment/Activity:
首先,您必须在 res/anim 目录
Slide_up.xml
Slide_bottom.xml
中创建两个动画资源,然后您必须创建一个样式
并将此行添加到您的类
中基于 http://www.devexchanges.info/2015/10/showing-dialog-with-animation-in-android .html
First, you have to create two animation resources in res/anim dir
slide_up.xml
slide_bottom.xml
then you have to create a style
and add this line to your class
Based in http://www.devexchanges.info/2015/10/showing-dialog-with-animation-in-android.html
我遇到了同样的问题,但是,最后我通过以下方式解决了问题
I meet the same problem,but ,at last I solve the problem by followed way
尝试下面的代码:
创建自定义动画:res/anim/customer_anim.xml:
Try below code:
Create the custom Anim.: res/anim/customer_anim.xml:
参考 https://stackoverflow.com/a/5591827/15614129
对话框打开和打开的动画close 将起作用,但如果您使用
[那么您将面临一些问题]
问题 - 如果用户在对话框外部单击,则对话框将在没有动画的情况下关闭
BugFix:我已将 fadeIn/fadeOut 动画更改为 leftToRight/rightToLeft(也在 AlertDialog 中) )
左到右.anim
右到左.anim
with ref to https://stackoverflow.com/a/5591827/15614129
Animation for dialog open & close will work but if you using
[then you ll be facing some issues]
Issue - If user clicks outside the dialogBox, then dialogBox will close without the animation
BugFix : I have changed the fadeIn/fadeOut animation to leftToRight/rightToLeft(Also in AlertDialog)
leftToRight.anim
rightToLeft.anim
在 kotlin 中:
对于从右到左(进入动画)和从左到右(退出动画):
styles.xml:
在 res/anim/ 中创建两个文件:
translate_right_side.xml:
在 Fragment/Activity 中:
In kotlin:
For right to left (entry animation) and left to right (exit animation):
styles.xml:
Create two files in res/anim/:
translate_right_side.xml:
In you Fragment/Activity: