18

Is there any way to inflate a view with WindowManager using Animation (at android's project)? I just can't do it even using the examples in sites! I used many examples but none worked!

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);
}
LeandroPortnoy
  • 312
  • 1
  • 2
  • 8

3 Answers3

40

I'm not sure about exact requirements for Your task, but there's two ways to provide animation to window:

  1. Use WindowManager.LayoutParams.windowAnimations like the following:

    params.windowAnimations = android.R.style.Animation_Translucent;
    
  2. Add additonal 'container' view, because WindowManager is not a real ViewGroup and so normal animation for adding views is not working with it. This question has been asked already, however it lacks the code. I would apply it the following way:

    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);
        }
    }
    
Community
  • 1
  • 1
sandrstar
  • 12,503
  • 8
  • 58
  • 65
  • sandrstar... worked perfectly! however... I wonder if it's possible using animation translation with these components. I need to make an effect up and down the screen with this component... – LeandroPortnoy Jul 22 '13 at 12:09
  • private void show(){ //Animation fadeIn = (Animation) AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in); //this.startAnimation(fadeIn); //this.bannerRelativeLayout.setVisibility(VISIBLE); this.setActive(true); mPopupLayout.setVisibility(VISIBLE); final Animation in = new TranslateAnimation(0, 0, -1000, 0 ); in.setDuration(700); AnimationSet animation = new AnimationSet(false); animation.addAnimation(in); mPopupLayout.startAnimation(animation); } – LeandroPortnoy Jul 22 '13 at 12:28
  • Sorry for delay. I've tried it and seems it works fine. May be You need to use params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = ViewGroup.LayoutParams.MATCH_PARENT; for FrameLayout. – sandrstar Jul 23 '13 at 02:45
  • @sandrstar Could you take a look at this question about WindowManager? Thank you http://stackoverflow.com/questions/27937250/viewpager-on-a-view-added-by-windowmanager-getting-java-lang-illegalargumentexc/27937559 – Hoa Vu Jan 15 '15 at 02:09
  • @sandrstar Why not animate the mParentView directly? – Run May 18 '21 at 08:37
  • @Run it'll look different than window animation – sandrstar May 19 '21 at 09:27
  • @sandrstar what's the difference? – Run May 20 '21 at 22:27
  • @Run as far as I remember, it might be about background behavour – sandrstar May 24 '21 at 13:11
0

Yes it is indeed possible. As long as the view you want to animate is inside a container, by container I mean for instance a LinearLayout or any other layout will do. Conclusively the view to be animated should not be the root view of a window and so you should be able to animate the view :) Hope it helps

xXJJJasonMokXx
  • 367
  • 4
  • 17
0

I want to add something when you want to add an animation to WindowManager via :

params.windowAnimations = android.R.style.Animation_Translucent;

the animation style must be a system resource, it can not be an application

A style resource defining the animations to use for this window. This must be a system resource; it can not be an application resource because the window manager does not have access to applications. Android Developers DOCUMENTATION