Android 按钮拖拽

先看看图:

里面主要是一个button,根据ontouchlistener来实现

主要实现代码:

bt.setOnTouchListener(object : OnTouchListener {
            //上次view的坐标位置
            internal var lastX: Int = 0
            internal var lastY: Int = 0
            var moveX = 0
            var moveY = 0
            var btnHeight = 0
            var btnWith = 0
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                // TODO Auto-generated method stub
                val ea = event.action
                Log.i("TAG", "Touch:$ea")

                when (ea) {
                    MotionEvent.ACTION_DOWN -> {
                        //按下记录view坐标
                        lastX = event.rawX.toInt()
                        lastY = event.rawY.toInt()
                        btnHeight = bt.height
                        btnWith = bt.width
                    }

                    MotionEvent.ACTION_MOVE -> {

                        //移动时记录相对上次的坐标
                        val dx = event.rawX.toInt() - lastX
                        val dy = event.rawY.toInt() - lastY

                        moveX = dx
                        moveY = dy
                        //相对于parent 的View上下左右位置
                        var left = v.left + dx
                        var top = v.top + dy
                        var right = v.right + dx
                        var bottom = v.bottom + dy

                        //如果left < 0,则是左移,右边框上次位置加上左移部分
                        if (left < 0) {
                            left = 0
                            right = left + v.width
                        }

                        //
                        if (right > screenWidth) {
                            right = screenWidth
                            left = right - v.width
                        }

                        //如果top < 0,则是上移,下边框上次位置加上移部分
                        if (top < 0) {
                            top = 0
                            bottom = top + v.height
                        }

                        if (bottom > screenHeight) {
                            bottom = screenHeight
                            top = bottom - v.height
                        }

                        //重新layout
                        v.layout(left, top, right, bottom)

                        Log.i("", "position$left, $top, $right, $bottom")

                        lastX = event.rawX.toInt()
                        lastY = event.rawY.toInt()
                    }
                    MotionEvent.ACTION_UP -> {
                        isDrag = Math.abs(moveX) == 0 && Math.abs(moveY) == 0

                        // 向四周吸附
                        var dx1 = event.rawX - lastX
                        var dy1 =event.rawY - lastY
                        var left1 = v.left + btnWith/2  + dx1
                        var top1 = v.top + dy1
                        var right1 = v .right + dx1
                        var bottom1 = v .bottom + dy1
                        if (left1 < (screenWidth / 2)) {
//                            when {
//                                top1 < 100 -> v.layout(left1.toInt(), 0, right1.toInt(), btnHeight)
//                                bottom1 > (screenHeight - 200) -> v.layout(left1.toInt(), (screenHeight - btnHeight), right1.toInt(), screenHeight)
//                                else ->
//                                    v.layout(0, top1.toInt(), btnHeight, bottom1.toInt())
//                            }
                            v.layout(0, top1.toInt(), btnWith, bottom1.toInt())
                        } else {
//                            when {
//                                top1 < 100 -> v.layout(left1.toInt(), 0, right1.toInt(), btnHeight)
//                                bottom1 > (screenHeight - 200) -> v.layout(left1.toInt(), (screenHeight - btnHeight), right1.toInt(), screenHeight)
//                                else -> v.layout((screenWidth - btnHeight), top1.toInt(), screenWidth, bottom1.toInt())
//                            }
                            v.layout((screenWidth - btnWith), top1.toInt(), screenWidth, bottom1.toInt())
                        }
                    }
                }
                return false
            }
        })

要理解里面怎么计算的还是要知道一些属性意思,本来想打算写的,犹豫太懒的原因,直接贴上别的博客里面讲的很详细,如果理解了,就不难 了。Android中View的getX,getY

github

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值