带动画的WindowManager(可能吗?)

时间:2013-07-19 11:42:49

标签: java android animation view window-managers

有没有办法用WindowManager使用Animation(在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);
}

2 个答案:

答案 0 :(得分:32)

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

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

答案 1 :(得分:0)

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