popupWindow设置显示和消失的动画 add:如何点击背景或返回键让popuwindow消失

来源:互联网 发布:工作证模板制作软件 编辑:程序博客网 时间:2024/04/25 21:24
首先定义显示效果的动画文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate  
        android:fromXDelta="0"  
        android:toXDelta="0"  
        android:fromYDelta="120"  
        android:toYDelta="0"  
        android:duration="500" />  
</set>

然后定义消失效果的动画文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate  
        android:fromXDelta="0"  
        android:toXDelta="0"  
        android:fromYDelta="0"  
        android:toYDelta="120"  
        android:duration="500" />  
</set>

PopupWindow的动画显示效果是通过setAnimationStyle(int id)方法设置的,其中id为一个style的id,所以我们要在styles.xml文件中设置一个动画样式:

<style name="popwin_anim_style">
     <item name="android:windowEnterAnimation">@anim/menushow</item>
     <item name="android:windowExitAnimation">@anim/menuhide</item>
</style>


add:
// the following 3 lines` codes are used to make sure the popupwindow can be closed when touch the other place
//out of the range of the popuewindow
//mWeatherPopuwindow.setFocusable(true);
//mWeatherPopuwindow.setOutsideTouchable(true);
//mWeatherPopuwindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
0 1