2013-07-19 64 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或任何其他布局都可以。最后,动画视图不应该是窗口的根视图,所以你应该可以为视图设置动画:)希望它有帮助