2014-02-22 44 views
0

我已在res/anim/中定義了一個PopupWindow和一個定義的XML縮放動畫,該動畫適用於顯示和關閉該彈出窗口。但情況是我想動態改變pivotXpivotY動畫的樣式,所以它從屏幕的不同點開始(增長)。在Android中以編程方式更改彈出式窗口動畫

因此,我可以在res/anim/in.xml中動態更改pivotXpivotY或創建新的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或任何我可以實現這樣的行爲。你能幫忙嗎?

回答

0

幾件事情來調整,以使你的代碼工作:

  • 設置寬度和高度爲 'popupView' 裏面的 'popupWindow'

  • 設置內插的ScaleAnimation

  • 或者使用

    popupView.startAnimation(scaleAnimation); 
    

    in的

    popupView.setAnimation(scaleAnimation); 
    

    代替或設置你的動畫的開始時間呼叫「setAnimation」之前,確保視圖的父被無效時的動畫效果開始

  • 取代

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

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

    其中寬度和高度必須大於t他在縮放過程中和之後的'popupView'的尺寸。通過LayoutParams.WRAP_CONTENT,ScaleAnimation將不起作用。

希望它有助於

相關問題