最近接到个需求,需要在用户与App交互时,把SDK中之前完成过的悬浮控件贴边躲藏,完毕交互后推迟一段时间再主动显现。本篇文章介绍一下完成的思路。

判别交互

用户与App交互、完毕交互可以经过监听接触事情来完成。主张运用的ActivitydispatchTouchEventActivity下的所有接触事情分发时都会回调此方法,代码如下:

class AutoEdgeHideActivity : BaseGestureDetectorActivity() {
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                // 手指按下,开始本次交互
                // 在此完成躲藏逻辑
            }
            MotionEvent.ACTION_UP -> {
                // 手指抬起,完毕本次交互
                // 在此完成推迟显现功能
            }
        }
        return super.dispatchTouchEvent(ev)
    }
}

躲藏与显现

想要完成的作用是当用户与App交互时,悬浮控件平移贴边,但保留一部分显现。完毕交互推迟一段时间后,悬浮控件平移回本来的方位。

此处经过ValueAnimator来完成,计算好控件的起始和完毕方位,然后改动控件的x坐标,代码如下:

private fun xCoordinateAnimator(view: View, startX: Float, endX: Float) {
    val animator = ValueAnimator.ofFloat(startX, endX)
    animator.addUpdateListener {
        // 不断更改控件的X坐标
        view.x = it.animatedValue as Float
    }
    // 设置插值器,速度由快变慢
    animator.interpolator = DecelerateInterpolator()
    // 设置动画的持续时间
    animator.duration = 500
    animator.start()
}

示例

整合之后做了个示例Demo,完整代码如下:

class AutoEdgeHideActivity : BaseGestureDetectorActivity() {
    private lateinit var binding: LayoutAutoEdgeHideActivityBinding
    private var widthPixels: Int = 0
    private val autoShowInterval = 2
    private var interacting = false
    private var hidden = false
    private var lastPositionX: Float = 0f
    private val handler = Handler(Looper.myLooper() ?: Looper.getMainLooper())
    private val autoShowRunnable = Runnable { autoShow() }
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.layout_auto_edge_hide_activity)
        widthPixels = resources.displayMetrics.widthPixels
        binding.includeTitle.tvTitle.text = "AutoEdgeHideExample"
        binding.vFloatView.setOnClickListener {
            if (hidden) {
                // 当前为躲藏状态,先显现
                // 把之前的推迟线程先撤销
                handler.removeCallbacks(autoShowRunnable)
                autoShow()
                Toast.makeText(this, "手动显现控件", Toast.LENGTH_SHORT).show()
            } else {
                // 相应正常的事情
                Toast.makeText(this, "点击了浮标控件", Toast.LENGTH_SHORT).show()
            }
        }
    }
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                if (!checkIsTouchFloatView(ev, binding.vFloatView)) {
                    // 起始ACTION_DOWN事情在浮标控件外,主动躲藏浮标控件,符号正在交互
                    interacting = true
                    handler.removeCallbacks(autoShowRunnable)
                    autoHide()
                }
            }
            MotionEvent.ACTION_UP -> {
                if (interacting) {
                    // 交互完毕,一定时间后主动显现,时间可以自由配置
                    interacting = false
                    handler.postDelayed(autoShowRunnable, autoShowInterval * 1000L)
                }
            }
        }
        return super.dispatchTouchEvent(ev)
    }
    /**
     * 查看是否接触浮标控件
     */
    private fun checkIsTouchFloatView(ev: MotionEvent, view: View): Boolean {
        val screenLocation = IntArray(2)
        view.getLocationOnScreen(screenLocation)
        val viewX = screenLocation[0]
        val viewY = screenLocation[1]
        return (ev.x >= viewX && ev.x <= (viewX + view.width)) && (ev.y >= viewY && ev.y <= (viewY + view.height))
    }
    private fun autoShow() {
        if (hidden) {
            hidden = false
            binding.vFloatView.let {
                xCoordinateAnimator(it, it.x, lastPositionX)
            }
        }
    }
    private fun autoHide() {
        if (!hidden) {
            hidden = true
            binding.vFloatView.let {
                // 记载一下显现状态下的x坐标
                lastPositionX = it.x
                // 躲藏时的x坐标,留一点控件的边缘显现(示例中默认控件在屏幕右侧)
                val endX = widthPixels - it.width * 0.23f
                xCoordinateAnimator(it, lastPositionX, endX)
            }
        }
    }
    private fun xCoordinateAnimator(view: View, startX: Float, endX: Float) {
        val animator = ValueAnimator.ofFloat(startX, endX)
        animator.addUpdateListener {
            view.x = it.animatedValue as Float
        }
        animator.interpolator = DecelerateInterpolator()
        animator.duration = 500
        animator.start()
    }
}

作用如图:

Android 控件自动贴边实现