动画自定义对话框 [英] Animate a custom Dialog

查看:213
本文介绍了动画自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个自定义对话框出现,就像它是从一个文本视图滑了下来。这可能吗?我似乎无法向任何动画应用到对话框类。我已经试过这条线的构造,但没有任何效果:

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

我甚至不知道如果动画是正确的,但我可以当我看到它在做什么调整。我将列出它下面为了完整性。我不是在寻找实际的动画,只是应用程序的对话框帮助。

paranimation.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<翻译
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:fromXDelta = -  200%
    机器人:toXDelta =0%
    机器人:fromYDelta =200%
    机器人:toYDelta =0%
    机器人:时间=3000
    机器人:zAdjustment =顶>
< /翻译>
 

解决方案

我一直在苦苦挣扎的今天与对话动画,终于得到了它的工作使用样式,所以这里是一个例子。

首先,最重要的事情 - 我可能有它的工作今天5种不同的方法,但不能说,因为......如果你的设备的动画设置设为无动画(设置→显示→动画),然后对话框将不会被动画不管你做什么!

以下是我的styles.xml的精简版。希望这是不言自明。这应该位于 RES /值

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <样式名称=PauseDialog父=@安卓风格/ Theme.Dialog>
        <项目名称=机器人:windowAnimationStyle> @风格/ PauseDialogAnimation< /项目>
    < /风格>

    <样式名称=PauseDialogAnimation>
        <项目名称=机器人:windowEnterAnimation> @动画/ spin_in< /项目>
        <项目名称=机器人:windowExitAnimation> @android:动画/ slide_out_right< /项目>
    < /风格>
< /资源>
 

windowEnterAnimation 是我的动画之一,位于水库\阿尼姆。 该 windowExitAnimation 是Android SDK的一部分的动画之一。

后来,当我在我的活动创建对话框 onCreateDialog(INT ID)方法,我这样做。

 对话对话框=新的对话框(这一点,R.style.PauseDialog);

//设置为对话框的标题和布局
dialog.setTitle(R.string.pause_menu_label);
dialog.setContentView(R.layout.pause_menu);
 

另外,您可以设置使用对话框构造函数,一个主题的动画通过以下方式来代替。

 对话对话框=新的对话框(本);
dialog.getWindow()的getAttributes()windowAnimations = R.style.PauseDialogAnimation。;
 

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>

解决方案

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.

<?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>

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.

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);

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆