美文网首页
实现悬浮在其他应用之上的view 定制Toast

实现悬浮在其他应用之上的view 定制Toast

作者: 西门小贼 | 来源:发表于2019-06-12 15:56 被阅读0次
    问题:

    要实现跳转到Google play的评分界面的悬浮引导,点击右上角X关闭弹窗。

    网上找了很多,没有找到合适的解决方式,通过反编译某流量应用代码,解析后实现,分享出来给有需要的小伙伴参考。时间问题,只做记录。

    注意点:

    1、View自己传入;
    2、显示时间可控;
    3、可交互;

    使用:
     ToastLocalUtil.makeText(getActivity().getApplicationContext(), 10000).show();
    
    public class ToastLocalUtil extends BaseToast {
        public ToastLocalUtil(Context context) {
            super(context);
        }
    
        public static BaseToast makeText(Context context, int time) {
            final ToastLocalUtil toast = new ToastLocalUtil(context);
            View inflate = LayoutInflater.from(context).inflate(R.layout.five_star_toast, null);//1、在此传入自己的布局文件
            (inflate.findViewById(R.id.iv_close)).setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    try {
                        if (toast != null) {
                            toast.hide();
                        }
                    } catch (Exception e) {
                    }
                }
            });
            toast.view = inflate;
            toast.time = time;
            toast.gravity = Gravity.BOTTOM; //2、调整显示位置
            return toast;
        }
    
        public void init(Context context) {
            super.init(context);
        }
    }
    
    public abstract class BaseToast {
        public int time = 2000;
        public int gravity = 48;
        public View view;
        protected final LayoutParams layoutParams = new LayoutParams();
        protected final Runnable showRunnable = new ShowRunnable();
        protected final Runnable hideRunnable = new HideRunnable();
        private final Handler handler = new Handler();
    
        private View mView;
        private WindowManager windowManager;
    
    
        class ShowRunnable implements Runnable {
            ShowRunnable() {
            }
    
            public void run() {
                BaseToast.this.handleShow();
            }
        }
    
        class HideRunnable implements Runnable {
            HideRunnable() {
            }
    
            public void run() {
                BaseToast.this.handleHide();
            }
        }
    
        public BaseToast(Context context) {
            init(context);
        }
    
    
        public void init(Context context) {
            LayoutParams layoutParams = this.layoutParams;
            layoutParams.height = -2;
            layoutParams.width = -1;
            layoutParams.flags = 136;
            layoutParams.format = -3;
            layoutParams.type = getWindowType();
            layoutParams.setTitle("Toast");
            this.windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        }
    
        //unknown type;
        public static int getWindowType() {
            if (Build.VERSION.SDK_INT > 25) {
                return 2038;
            }
            if (Build.VERSION.SDK_INT >= 19) {
                return 2005;
            }
            return 2002;
        }
    
        public void handleShow() {
            if (this.mView != this.view) {
                handleHide();
                this.mView = this.view;
                int gravity = this.gravity;
                this.layoutParams.gravity = gravity;
                //unknown num Gravity.*;
                if ((gravity & 7) == 7) {
                    this.layoutParams.horizontalWeight = 1.0f;
                }
                if ((gravity & 112) == 112) {
                    this.layoutParams.verticalWeight = 1.0f;
                }
                if (this.mView.getParent() != null) {
                    this.windowManager.removeView(this.mView);
                }
                try {
                    this.windowManager.addView(this.mView, this.layoutParams);
                } catch (Exception e) {
                }
            }
        }
    
        public void handleHide() {
            if (this.mView != null) {
                if (this.mView.getParent() != null) {
                    this.windowManager.removeView(this.mView);
                }
                this.mView = null;
            }
        }
    
        public void show() {
            this.handler.post(this.showRunnable);
            if (this.time > 0) {
                this.handler.postDelayed(this.hideRunnable, (long) this.time);
            }
        }
    
        public void hide() {
            this.handler.post(this.hideRunnable);
        }
    

    相关文章

      网友评论

          本文标题:实现悬浮在其他应用之上的view 定制Toast

          本文链接:https://www.haomeiwen.com/subject/qeplfctx.html