【问题标题】:WindowManager with Animation (is it possible?)带有动画的 WindowManager(可能吗?)
【发布时间】:2013-07-18 16:35:46
【问题描述】:

有没有办法使用 WindowManager 使用动画(在 android 的项目中)为视图充气?即使使用站点中的示例,我也无法做到!我用了很多例子,但没有一个奏效!

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);
}

【问题讨论】:

    标签: java android animation view window-managers


    【解决方案1】:

    我不确定您的任务的确切要求,但有两种方法可以为窗口提供动画:

    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);
          }
      }
      

    【讨论】:

    • sandrstar... 完美运行!但是...我想知道是否可以对这些组件使用动画翻译。我需要用这个组件在屏幕上下产生效果...
    • private void show(){ //动画fadeIn = (Animation) AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in); //this.startAnimation(fadeIn); //this.bannerRelativeLayout.setVisibility(VISIBLE); this.setActive(true); mPopupLayout.setVisibility(VISIBLE);最终动画 in = new TranslateAnimation(0, 0, -1000, 0); in.setDuration(700); AnimationSet 动画 = new AnimationSet(false);动画.addAnimation(in); mPopupLayout.startAnimation(动画); }
    • 抱歉耽搁了。我已经尝试过了,似乎效果很好。可能是你需要使用 params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = ViewGroup.LayoutParams.MATCH_PARENT;对于框架布局。
    • @sandrstar 你能看看这个关于 WindowManager 的问题吗?谢谢stackoverflow.com/questions/27937250/…
    • @sandrstar 为什么不直接为 mParentView 设置动画?
    【解决方案2】:

    是的,这确实是可能的。只要您想要动画的视图在容器内,容器我的意思是例如 LinearLayout 或任何其他布局都可以。最后,要动画的视图不应该是窗口的根视图,因此您应该能够为视图设置动画:)希望它有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2011-07-29
      相关资源
      最近更新 更多