android PopupWindow

View loadingView = this.getLayoutInflater().inflate(R.layout.loading, null);
PopupWindow loaddingPopup = new PopupWindow(loadingView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
loaddingPopup.setAnimationStyle(R.style.popupWindow); //配置动画
loaddingPopup.update();
loaddingPopup.showAtLocation(findViewById(R.id.btnRefresh), Gravity.CENTER, 0, 0); //显示在中间

配置一个animate, popupwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

除了要配置一个animate外,还需要配置style

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <style name="popupWindow">  
        <item name="android:windowEnterAnimation">@anim/popupwindow</item>  
    </style>  
</resources>  

容易犯错的地方是,这里的setAnimationStyle是指style,不是动画

popupWindow.setAnimationStyle(R.anim.popupwindow);  // 错误写法 

popwindow通过setAnimationStyle(int animationStyle)函数来设置动画效果
android:windowEnterAnimation表示进入窗口动画
android:windowExitAnimation表示窗口退出动画 更多信息:android PopupWindow 以及activity切换的动画效果对比

updatedupdated2023-12-062023-12-06