2013-07-19 108 views
12

有沒有什麼辦法使用動畫(在android的項目)使用WindowManager膨脹視圖?即使使用站點中的示例,我也無法做到!我用了很多例子,但都沒有工作!WindowManager動畫(有可能嗎?)

public BannerLayout(Activity activity, final Context context) { 
    super(context); 

    this.context = context; 

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams( 
      WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
      WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 
      WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
      PixelFormat.TRANSLUCENT); 

    wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    this.popupLayout = (RelativeLayout) inflater.inflate(R.layout.popup_activity, null); 
    this.popupLayout.setVisibility(GONE); 
    this.setActive(false); 

    wm.addView(this.popupLayout, params); 

    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 


private void show(){ 
    Animation in = AnimationUtils.loadAnimation(this.context, android.R.anim.fade_in); 
    this.popupLayout.setAnimation(in); 

    this.setActive(true); 
    this.popupLayout.setVisibility(VISIBLE); 
} 

回答

28

我不知道關於你的任務的具體要求,但有兩種方法提供動畫窗口:

  1. 使用WindowManager.LayoutParams.windowAnimations類似如下:

    params.windowAnimations = android.R.style.Animation_Translucent; 
    
  2. 添加額外的「容器」視圖,因爲WindowManager不是真正的ViewGroup,因此用於添加視圖的普通動畫不適用於此視圖。 This question has been asked already,但它缺少代碼。我會申請它通過以下方式:

    public class BannerLayout extends View { 
    
        private final Context mContext; 
    
        private final ViewGroup mPopupLayout; 
    
        private final ViewGroup mParentView; 
    
        public BannerLayout(Activity activity, final Context context) { 
         super(context); 
    
         mContext = context; 
    
         final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
           WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
           WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 
             WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
           PixelFormat.TRANSLUCENT); 
    
         final WindowManager mWinManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    
         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         mPopupLayout = (RelativeLayout) inflater.inflate(R.layout.popup_activity, null); 
         mPopupLayout.setVisibility(GONE); 
    
         params.width = ActionBar.LayoutParams.WRAP_CONTENT; 
         params.height = ActionBar.LayoutParams.WRAP_CONTENT; 
    
         // Default variant 
         // params.windowAnimations = android.R.style.Animation_Translucent; 
    
         mParentView = new FrameLayout(mContext); 
    
         mWinManager.addView(mParentView, params); 
    
         mParentView.addView(mPopupLayout); 
         mPopupLayout.setVisibility(GONE); 
        } 
    
        /** 
        * Shows view 
        */ 
        public void show(){ 
         final Animation in = AnimationUtils.loadAnimation(this.mContext, android.R.anim.fade_in); 
    
         in.setDuration(2000); 
    
         mPopupLayout.setVisibility(VISIBLE); 
         mPopupLayout.startAnimation(in); 
        } 
    
        /** 
        * Hides view 
        */ 
        public void hide() { 
         mPopupLayout.setVisibility(GONE); 
        } 
    } 
    
+0

sandrstar ...完美地工作!然而......我想知道是否有可能使用這些組件進行動畫翻譯。我需要的效果上下屏幕與此組件... – LeandroPortnoy

+0

私人無效顯示(){ \t \t \t \t //動畫淡入=(動畫)AnimationUtils.loadAnimation(的getContext(),android.R。 anim.fade_in); \t \t //this.startAnimation(fadeIn); \t //this.bannerRelativeLayout.setVisibility(VISIBLE); \t \t \t \t this.setActive(true); mPopupLayout.setVisibility(VISIBLE); \t \t final Animation = in new TranslateAnimation(0,0,-1000,0); in.setDuration(700); AnimationSet animation = new AnimationSet(false); animation.addAnimation(in); \t mPopupLayout.startAnimation(animation); \t} – LeandroPortnoy

+0

對不起。我試過了,似乎它工作正常。可能是你需要使用params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = ViewGroup.LayoutParams.MATCH_PARENT;爲FrameLayout。 – sandrstar

0

是的,這確實是可能的。只要你想要動畫的視圖在一個容器內,容器的意思是例如一個LinearLayout或任何其他佈局都可以。最後,動畫視圖不應該是窗口的根視圖,所以你應該可以爲視圖設置動畫:)希望它有幫助