PopupWindow添加动画

来源:互联网 发布:阿里云服务器安装镜像 编辑:程序博客网 时间:2024/06/17 09:28

PopupWindow添加动画

为PopupWindow添加动画,效果如图:
这里写图片描述

点击展开动画

设置popup显示动画R.anim.anim_popup_in

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/overshoot_interpolator">    <scale        android:duration="300"        android:fillAfter="true"        android:fromXScale="0.2"        android:toXScale="1.0"        android:fromYScale="0.2"        android:toYScale="1.0"        android:pivotX="0"        android:pivotY="0"></scale></set>

android:anim/overshoot_interpolator:向前甩一定值后再回到原来位置

点击关闭动画

关闭Popup的动画R.anim.anim_popup_out

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/anticipate_interpolator">    <scale        android:duration="300"        android:fillAfter="true"        android:fromXScale="1.0"        android:fromYScale="1.0"        android:toXScale="0.2"        android:toYScale="0.2"></scale></set>

android:anim/anticipate_interpolator 动画开始的时候向后甩一定距离进行动画

PopupWindow设置动画

popupwindow设置动画提供了方法

public void setAnimationStyle(int animationStyle) {}

从这个方法可以看出来我们需要提供一个的style,我们自定义一个style
设置进入动画和移除动画

<style name="MyPopupStyle" >        <!-- 进入动画 -->        <item name="android:windowEnterAnimation">@anim/anim_popup_in</item>        <!-- 移除动画 -->        <item name="android:windowExitAnimation">@anim/anim_popup_out</item>    </style>

最后给出PopupWindow的简单创建代码

PopupWindow mPopupWindow = new PopupWindow();View view = LayoutInflater.from(this).inflate(R.layout.include_test,null);mPopupWindow.setContentView(view);// 设置SelectPicPopupWindow弹出窗体的宽       mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置SelectPicPopupWindow弹出窗体的高     mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);        mPopupWindow.setAnimationStyle(R.style.MyPopupStyle);

最后出来的效果就是上图所示效果

0 0
原创粉丝点击