弹出窗口动画在Android中以编程方式更改

时间:2014-02-22 15:53:43

标签: android animation

我已经在PopupWindow中有一个res/anim/和一个已定义的XML缩放动画,该动画应用于show和取消该弹出窗口。但情况是我想动态更改动画风格的pivotXpivotY,以便从屏幕的不同点开始(增长)。

因此,我可以在pivotX中动态更改pivotYres/anim/in.xml,也可以创建新的ScaleAnimation并应用它。我看不出如何做第一个选项,不幸的是后者在我写的时候不起作用:

public class MyPopup {

    private PopupWindow popupWindow;

    private Integer growFromX;
    private Integer growFromY;

    public MyPopup(Integer growFromX, Integer growFromY) {
        if (growFromX != null && growFromY != null) {
            this.growFromX = growFromX.intValue();
            this.growFromY = growFromY.intValue();
        }
    }

    public void show() {
        LayoutInflater layoutInflater = (LayoutInflater) this.activity
                .getBaseContext().getSystemService(
                        Activity.LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(
                R.layout.map_selector_dialog_layout, null);

        ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 0.0f,
                1.0f, 1.0f, Animation.RELATIVE_TO_SELF,
                (float) this.growFromX, Animation.RELATIVE_TO_SELF,
                (float) this.growFromY);
        scaleAnimation.setFillAfter(false);
        scaleAnimation.setDuration(4000);
        popupView.setAnimation(scaleAnimation);

        this.popupWindow = new PopupWindow(popupView, 
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        this.popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
    }

}

它不一定是WindowPopup。我可以使用Dialog或其他任何我可以实现这样的行为。你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

要调整以使代码正常工作的一些事情:

  • 在'popupWindow'中设置'popupView'的宽度和高度

  • 为ScaleAnimation设置内插器

  • 使用

    popupView.startAnimation(scaleAnimation);
    

    而不是

    popupView.setAnimation(scaleAnimation);
    

    或在调用'setAnimation'之前设置动画的开始时间,并确保在动画开始时视图的父项无效

  • 替换

    this.popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    

    this.popupWindow = new PopupWindow(popupView, width, height);
    

    在缩放过程中和之后,宽度和高度必须大于'popupView'的尺寸。使用LayoutParams.WRAP_CONTENT,ScaleAnimation将无效。

希望有所帮助