自定义对话框中的动画无法正常工作

7

我已经按照链接创建了自定义对话框,并且一切都运行得非常完美。但是,我想添加动画效果,使其看起来像从屏幕顶部到底部滑动。所以我搜索了这两个动画并将它们放在了anim文件夹中。为了在我的自定义对话框中应用它们,我稍微修改了构造函数。我在自定义对话框的构造函数中添加了这一行:

 public AnimationDialog(Activity a, int drawable) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
    this.mDrawable = drawable;
    this.getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnim;
}

以下是我添加的代码,以实现上述动画效果:

this.getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnim;

但是没有任何反应,我的对话框仍然以默认方式出现。

下面是我的样式文件供参考:

 <style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_down_animation</item>
    <item name="android:windowExitAnimation">@anim/slide_up_animation</item>
</style>

<!-- Animation for dialog box -->
<style name="DialogSlideAnim" parent="@android:style/Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style>

但是我的动画仍然不能正常工作,我做错了什么?请告诉我如何实现这个动画?如何为我的自定义对话框添加动画效果。
编辑:
这是我的下滑动画。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="0%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="100%p" />

</set>

这是我的滑动动画

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="100%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="0" />
</set>

你在哪里设置你的动画样式? - Android Android
在构造函数中,使用以下代码:this.getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnim; - Coas Mckey
你确定你没有混淆两个动画资源(名称似乎是这样的...)吗?请向我们展示你的动画资源。 - Bartek Lipinski
@BartoszLipinski的问题已编辑 - Coas Mckey
3个回答

7
请尝试这个:
滑动下降动画.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="-100%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="0%p" />
</set>

and

slide_up_animation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="0%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="-100%p" />
</set>

补充:

除此之外,您还可以尝试通过以下方式设置您的样式:

getWindow().setWindowAnimations(R.style.DialogAnimation);

(而不是R.style.DialogSlideAnim)


你有尝试过我在答案中发布的所有内容吗? - Bartek Lipinski
但它非常快速?如何控制它的速度。 - Coas Mckey
在您的动画资源中更改 android:duration 参数。您可以在那里使用毫秒。例如,您可以使用 android:duration="600" 而不是 android:duration="@android:integer/config_mediumAnimTime" - Bartek Lipinski
android:duration="@android:integer/config_mediumAnimTime" 这个时间你认为是多少? - Coas Mckey
你应该能够通过按住Ctrl/Command键并单击config_mediumAnimTime来检查它。它是400毫秒。 - Bartek Lipinski

2
在onStart()回调函数中的style.xml中添加对话框片段的动画。
@Override
public void onStart() {
  super.onStart();

  if (getDialog() == null) {
    return;
  }

  // set the animations to use on showing and hiding the dialog
  getDialog().getWindow().setWindowAnimations(R.style.DialogAnimation);

}

1

试试这个:

slide_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="0"
        android:toYDelta="100%" />
</set>

slide_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="100%"
        android:toYDelta="0" />
</set>

在 style.xml 中添加以下样式:
 <style name="DialogStyle" 
    parent="Theme.MaterialComponents.DayNight.Dialog.Bridge">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorPrimary">@color/colorPrimary</item>

    <!-- Additionally if you want animations when dialog opening -->
    <item name="android:windowEnterAnimation">@anim/slide_up</item>
    <item name="android:windowExitAnimation">@anim/slide_down</item>
</style>

在对话框开始处添加以下内容

 @Override
public void onStart() {
    super.onStart();
    if (getDialog() == null||getDialog().getWindow() == null) {
        return;
    }
    getDialog().getWindow().setWindowAnimations(R.style.DialogStyle);

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接