Day23 - 调整悬浮视窗大小

今天来做调整视窗大小的功能。

因为今天比较累,我内容会写比较少。

OnClickListener

binding.resize.setOnClickListener {
    if (it.isSelected) {
        binding.touchPoint.visibility = View.GONE
        binding.leftTopPoint.setOnTouchListener(null)
        binding.leftBotPoint.setOnTouchListener(null)
        binding.rightTopPoint.setOnTouchListener(null)
        binding.rightBotPoint.setOnTouchListener(null)
        updateHandler.post(updateRunnable)
    } else {
        updateHandler.removeCallbacks(updateRunnable)
        binding.touchPoint.visibility = View.VISIBLE
        binding.leftTopPoint.setOnTouchListener(onFrameResizeListener)
        binding.leftBotPoint.setOnTouchListener(onFrameResizeListener)
        binding.rightTopPoint.setOnTouchListener(onFrameResizeListener)
        binding.rightBotPoint.setOnTouchListener(onFrameResizeListener)
    }
    binding.recyclerView.touchable = it.isSelected
    binding.move.isClickable = it.isSelected
    binding.close.isClickable = it.isSelected
    binding.back.isClickable = it.isSelected
    it.isSelected = !it.isSelected
}

Day22中的移动视窗差不多的内容,把其他按钮的状态设一设。leftTopPointleftBotPointrightTopPointrightBotPoint是我分别在Layout四个角落放上的圆点ImageView,最後一节的目前画面能看到。另外开始调整大小时要注意把updateRunnable给取消掉,否则可能会和RecyclerView的画面更新冲突导致Crash。

onFrameResizeListener

private val onFrameResizeListener = View.OnTouchListener { v, motionEvent ->
    val params = binding.root.layoutParams as WindowManager.LayoutParams
    when (motionEvent.action) {
        MotionEvent.ACTION_DOWN -> {
            return@OnTouchListener true
        }
        MotionEvent.ACTION_MOVE -> {
            if (motionEvent.historySize < 2) return@OnTouchListener true
            var dx = motionEvent.getHistoricalX(1) - motionEvent.getHistoricalX(0)
            var dy = motionEvent.getHistoricalY(1) - motionEvent.getHistoricalY(0)
            params.x = (params.x + dx).toInt()
            params.y = (params.y + dy).toInt()

            when (v) {
                binding.leftTopPoint -> {
                    dx = -dx
                    dy = -dy
                }
                binding.leftBotPoint -> {
                    dx = -dx
                }
                binding.rightTopPoint -> {
                    dy = -dy
                }
            }
            params.width = (params.width + dx).toInt()
            params.height = (params.height + dy).toInt()

            windowManager.updateViewLayout(binding.root, params)
            return@OnTouchListener true
        }
    }

    return@OnTouchListener false
}

基本上内容也和Day22中移动视窗的部分大同小异,唯一的差别是在移动画面的同时去修改画面的大小。当然目前这部分的效果还是很粗糙,可能等後续其他功能都做得差不多再来调整了。

目前画面

https://i.imgur.com/yhSrq1k.gif


<<:  【从零开始的Swift开发心路历程-Day26】将起始画面设为XIB

>>:  今晚来聊聊铼德2349

Day03-入口管制(二)

前言 昨天在讲格式验证的时候有提到 Node.js 的 validator 跟 Go 的 goval...

DAY18-动态规划(一)

力扣网站的说明 动态规划常常适用于有重叠子问题和最优子结构性质的问题,并且记录所有子问题的结果,因此...

python3-日历

在python3中,想要制作日历有两种方式,先介绍第一种: -直接使用python中calendar...

Day 30 我完成铁人了!

心得 哈罗大家,今天是铁人挑战第三十天,终於来到最後一天了,老实说我真的没想过我会参加这个比赛,更没...

Day17-238. Product of Array Except Self

今日题目:238. Product of Array Except Self Given an in...