Andriod中为Dialog设置动画
2016-12-05 00:27
483 查看
因为Dialog不属于View,所以不能使用View.startAnimation()。
看了Dialog的源码发现,Dialog其实是Window实现的。所以我们可以使用Window设置动画的方式来实现。
我们这里使用AlertDailog,实现从顶部弹入,隐藏时回到顶部消失。
anim_out
windowExitAnimation是隐藏时的动画
看了Dialog的源码发现,Dialog其实是Window实现的。所以我们可以使用Window设置动画的方式来实现。
我们这里使用AlertDailog,实现从顶部弹入,隐藏时回到顶部消失。
首先定义2个动画xml
anim_in<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500"> <translate android:fromYDelta="-50%p" android:toYDelta="0" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
anim_out
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500"> <translate android:fromYDelta="0" android:toYDelta="-50%p" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" /> </set>
增加一个样式,引用2个动画
windowEnterAnimation是显示时的动画windowExitAnimation是隐藏时的动画
<style name="CustomDialog"> <item name="android:windowEnterAnimation">@anim/anim_in</item> <item name="android:windowExitAnimation">@anim/anim_out</item> </style>
为AlertDialog添加动画
//创建builder AlertDialog.Builder builder = new AlertDialog.Builder(this) .setMessage("message").setTitle("标题") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); //创建AlertDialog AlertDialog alertDialog = builder.create(); //获取Diloag所在的Window Window window = alertDialog.getWindow(); //为Window设置动画 window.setWindowAnimations(R.style.CustomDialog); //显示Dialog alertDialog.show();
相关文章推荐
- Android 动画之AlphaAnimation应用详解
- Android实现动画效果详解
- Android动画之3D翻转效果实现函数分析
- BaseAnimation是基于开源的APP,致力于收集各种动画效果(最新版本1.2)
- 关于TranslateAnimation()的参数问题
- Android的屏幕切换动画
- Android动画学习笔记-Android Animation
- Android动画笔记
- Android动画之translate(位移动画)
- android动画体系
- Android动画之Drawable Animation
- 值动画
- Android常用动画
- Android布局动画和逐帧动画
- 收集android上开源的酷炫的交互动画和视觉效果
- 动画的简单介绍
- Android动画(实现抛物线运动)
- Android控件动画
- Android学习笔记第四篇--图像与动画
- Interpolator的几种属性