以编程方式将动画效果添加到android中的(以编程方式添加的)popupWindow

时间:2014-11-26 11:02:31

标签: android popupwindow

所以,我有一个以编程方式添加的PopupWindow,如下所示:

        dialog = new PopupWindow(context);
        dialog.setContentView(ll);
        dialog.showAtLocation(view, Gravity.LEFT | Gravity.TOP, -70, 0);
        dialog.setWidth(w);
        dialog.setHeight(h - 50);
        dialog.setOutsideTouchable(true);
        //The dialog.update is somewhere else, I didn't bother adding it too as it is not important for this matter (I guess)

我想要做的是拥有某种动画效果,就像它从我按下的按钮弹出一样,弹出窗口。 (这只是一个例子,我只想要任何类型的动画)。

文档也可以,只要它不是基于XML的(我发现那些已经不是真的帮助我了。)

如果需要其他详细信息,我会评论或编辑问题。

2 个答案:

答案 0 :(得分:14)

所以,我设法解决了这个问题。

实现动画效果有三个简单的步骤。

第一:     制作两个动画的XML。在我的例子中,这两个跟在这里。   animation_on.xml

 <scale xmlns:android="http://schemas.android.com/apk/res/android"
  android:toXScale="1.0"              
  android:fromXScale="0.0"            

  android:toYScale="1.0"
  android:fromYScale="0.0"

  android:pivotX="0%"
  android:pivotY="50%"


  android:startOffset="100"
  android:duration="300" />

animation_off.xml

   <scale xmlns:android="http://schemas.android.com/apk/res/android"
   android:toXScale="0.0"              
   android:fromXScale="1.0"            

   android:toYScale="0.0"
   android:fromYScale="1.0"

   android:pivotX="0%"
   android:pivotY="50%"


   android:startOffset="100"
   android:duration="300" />

第二

<style name="animationName" parent="android:Animation">
    <item name="android:windowEnterAnimation">@anim/animation_on</item>
    <item name="android:windowExitAnimation">@anim/animation_off</item>
</style>

第三

 dialog = new PopupWindow(context);
 // ....other code, whatever you want to do with your popupWindow (named dialog in our case here)
 dialog.setAnimationStyle(R.style.animationName);

如果有人需要帮助,请留言。我会尽快回答。

答案 1 :(得分:9)

以下是设置动画风格的代码。确保在调用showAtLocation之前调用setAnimationStyle方法。

dialog = new PopupWindow(context);
dialog.setAnimationStyle(android.R.style.Animation_Dialog);

希望这有帮助。

参考:setAnimationStyle