我已经按照链接创建了自定义对话框,并且一切都运行得非常完美。但是,我想添加动画效果,使其看起来像从屏幕顶部到底部滑动。所以我搜索了这两个动画并将它们放在了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>