android - 动画在自定义对话框中不起作用

标签 android android-animation android-dialog customdialog

我在这个 link 之后制作了自定义对话框它工作得很好。但后来我想添加动画,让它看起来像是从屏幕的底部到底部。所以我搜索了这两个动画并找到了它们并将它们放在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>

最佳答案

试试这个:

slide_down_animation.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>

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)

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

相关文章:

java - 使用一个 APK 更改为两个环境

android - 将 RIGHT 外连接转换为左外连接 SQLite

android - 在 Android 中以编程方式创建 ImageView

Android重新显示来自单独 fragment 的dialogfragment

android - 从相机和画廊获取照片

android - 如何取消场景转换动画?

android - 为整个布局设置动画

android - 显示对话框代码未执行

android - 在 Android 中使用 DatePicker 时遇到问题

java - 尝试在 Android Studio 中实现 getSharedPreferences 时出现 "cannot resolve method"错误