17

有没有办法使用动画(在android的项目中)使用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);
}
4

2 回答 2

38

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

  1. WindowManager.LayoutParams.windowAnimations像下面这样使用:

    params.windowAnimations = android.R.style.Animation_Translucent;
    
  2. 添加额外的“容器”视图,因为WindowManager它不是真实的ViewGroup,因此添加视图的正常动画无法使用它。这个问题已经被问过了,但是它缺少代码。我将通过以下方式应用它:

    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);
        }
    }
    
于 2013-07-20T05:03:59.387 回答
0

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

于 2014-02-19T14:09:24.040 回答