首先,在values/anim中定义补间动画
popshow_anim.xml 由下往上淡入
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
pophidden_anim.xml 由上往下淡出
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
然后,在values/style中定义样式
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item> <!-- 指定显示的动画xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item> <!-- 指定消失的动画xml -->
</style>
最后,在代码中引用动画样式
View view = LayoutInflater.from(this).inflate(R.layout.popwindowlayout, null);
PopupWindow mPopupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0xcc00cc00));
// 设置popWindow的显示和消失动画
mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
mPopupWindow.showAtLocation(MainActivity.this.findViewById(R.id.start), Gravity.BOTTOM, 0, 0);