带有动画的窗口管理器(可能吗?

2022-09-02 23:49:08

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

答案 1

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

  1. 按如下方式使用:WindowManager.LayoutParams.windowAnimations

    params.windowAnimations = android.R.style.Animation_Translucent;
    
  2. 添加附加的“容器”视图,因为不是真正的,因此添加视图的正常动画不适用于它。这个问题已经被问过了,但是它缺少代码。我将按以下方式应用它:WindowManagerViewGroup

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

答案 2

是的,这确实是可能的。只要您要制作动画的视图位于容器内,例如,通过容器,我的意思是线性布局或任何其他布局都可以。最后,要进行动画处理的视图不应是窗口的根视图,因此您应该能够对视图进行动画处理,希望它有所帮助:)


推荐