WindowManager with Animation (is it possible?)

前端 未结 2 803
野的像风
野的像风 2020-12-08 01:30

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

相关标签:
2条回答
  • 2020-12-08 01:42

    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

    0 讨论(0)
  • 2020-12-08 01:59

    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);
          }
      }
      
    0 讨论(0)
提交回复
热议问题