android实现popupwindow的动画效果

问题:在打开或者关闭popupwindow的时候怎么样显示动画效果?
参考了launcher里面关于页面缩略图的popupwindow打开时的动画效果,具体实现如下:
在我之前的例子上添加动画效果,例子在这儿可以看到:http://gqdy365.iteye.com/blog/994746
在anim中定义两个动画文件,一个是在打开popupwindow时使用的,一个是关闭时使用的。我打开的动画效果如下:
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <scale
          android:interpolator="@android:anim/accelerate_decelerate_interpolator"
         
          android:fromXScale="0.0"
          android:toXScale="1.0"
         
          android:fromYScale="0.0"
          android:toYScale="1.0"
         
          android:pivotX="50%"
          android:pivotY="50%"
         
          android:fillAfter="false"
          android:duration="700" />
</set>
是一个缩放效果,弹出时从最小逐渐展开!关闭的动画可以参考上面的代码。
popupwindow的效果要在style中定义相应的动画并引用刚才定义的动画文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AnimationPreview">
<item name="android:windowEnterAnimation">@anim/fade_in</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
</resources>
其中fade_in和fade_out是刚才在anim中定义的文件。
完成上述定义之后就可以在代码中引用了:

PopupWindow window = new PopupWindow(v, 500,260);

//设置整个popupwindow的样式。
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop));
//使窗口里面的空间显示其相应的效果,比较点击button时背景颜色改变。
//如果为false点击相关的空间表面上没有反应,但事件是可以监听到的。
//listview的话就没有了作用。
window.setAnimationStyle(R.style.AnimationPreview);
window.setFocusable(true);
window.update();
window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 0);

其中主要是window.setAnimationStyle(R.style.AnimationPreview);这一句给popupwindow添加了动画样式。

效果图我就不截图了,很难截取的!

猜你喜欢

转载自z303729470.iteye.com/blog/1783055