前言
PopWindow工具类
import android.app.Activity
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.view.Display
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.PopupWindow
/***
* PopWindowUtil
* Created by zxjie on 2021/5/17.
* address:bailingkeji
* describe:使用时请先调用init进行初始化,之后调用showPopWindow进行显示,该工具类采用单例设计模式
* link:https://blog.csdn.net/weixin_46603990/article/details/116987681
* https://juejin.cn/post/6963513638783205406/
* example: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* | -实例化对象-
* | private val popWindowUtil = PopWindowUtil
* |
* | -初始化PopWindow-
* | --设置固定大小
* | popWindowUtil.init(this,R.layout.pop_alert_two,295F,135F,null)
* | --适应屏幕最大化
* | popWindowUtil.init(this,R.layout.pop_alert_two,PopWindowUtil.MATCH,PopWindowUtil.MATCH,null)
* | --基于屏幕设置宽高,,Restraint中的参数只有在前者为CHANGE时才会生效,公式为 屏幕宽高-Restraint.width/height
* | popWindowUtil.init(this,R.layout.pop_alert_two,PopWindowUtil.CHANGE,PopWindowUtil.CHANGE,Restraint(20,60))
* | --组合使用,宽置满,高基于屏幕设置
* | popWindowUtil.init(this,R.layout.pop_alert_two,PopWindowUtil.MATCH,PopWindowUtil.CHANGE,Restraint(0,60))
* |
* | -获取所用到的控件
* | val btn_ok = popWindowUtil.getViewById(R.id.btn_ok) as TextView
* |
* | -设置点击窗口外边,窗口消失,默认为false
* | popWindowUtil.outSideTouchable(true)
* |
* | -获取焦点,默认为false
* | popWindowUtil.itemClickable(true)
* |
* | -使用默认的阴影效果,默认为false
* | popWindowUtil.shadowShowAble(true)
* |
* | -设置PopWindow消失监听
* | popWindowUtil.dismissListener = {
* | -自定义背景
* | }
* |
* | -弹出PopWindow 注意:弹出的PopWindow永远不会跃出屏幕
* | --基于整个屏幕居中弹出,向下偏移10dp
* | popWindowUtil.showPopWindow(toolbar,true, Excursion(Gravity.CENTER,0,10),null)
* | --基于整个屏幕在左上弹出,并向左偏移10dp 注意:这时候PopWindow已经显示在屏幕最左侧了,所以这个-10是无效的
* | popWindowUtil.showPopWindow(toolbar,true, Excursion(Gravity.LEFT or Gravity.TOP,-10,0),null)
* | --基于整个屏幕从下方滑动弹出,并在tabLayout上方
* | popWindowUtil.showPopWindow(tab_layout, true, Excursion(Gravity.BOTTOM, 0, DensityUtil.px2dip(this, (tab_layout.layoutParams.height+nbHeight-4) * 1F)),
* | --注意偏移的方向并不是正右下,负左上,它是根据位置约束来决定的,位置约束为BOTTOM,那么就是正上,负下,如果为TOP,就是正下,负上,x轴同理
* | --不建议使用 基于toolbar控件的左下弹出 注意:为false时位置约束(Gravity.CENTER)将会失效
* | popWindowUtil.showPopWindow(toolbar,false, Excursion(Gravity.CENTER,0,0),null)
* |
* | -建议设置PopWindow弹出时按返回键关闭PopWindow,而不是关闭Activity,注意:onBackPressed()方法只有在Activity中才能重写
* | override fun onBackPressed() {
* | if (!popWindowUtil.disMiss()) finish()
* | }
* |
* | -关闭PopWindow
* | popWindowUtil.disMiss()
* |
* | -销毁PopWindow所使用的的对象,在onDestroy调用
* | popWindowUtil.destroy()
* |
* | -当工具类提供的方法不满足你的需求时可以调用以下方法获取PopWindow对象
* | popWindowUtil.getPopWindow()
* |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
object PopWindowUtil {
private var winHeight: Int = 0
private var winWidth: Int = 0
private var view: View? = null
private var popupWindow: PopupWindow? = null
private var context: Context? = null
private var canDown: Boolean = false
private var canClick: Boolean = false
private var canShow: Boolean