View实现滑动的方式

1.使用View.layout()方法

class CustomView : View {
    var downX = 0f
    var downY = 0f

    constructor(context: Context?) : this(context, null, 0)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        setOnTouchListener { v, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    downX = event.x
                    downY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    val offsetX = event.x - downX
                    val offsetY = event.y - downY
                    layout(
                        (left + offsetX).toInt(),
                        (top + offsetY).toInt(),
                        (right + offsetX).toInt(),
                        (bottom + offsetY).toInt()
                    )
                }
            }
            true
        }
    }


}

2.使用offsetLeftAndRight、offsetTopAndBottom

class CustomView : View {
    var downX = 0f
    var downY = 0f

    constructor(context: Context?) : this(context, null, 0)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        setOnTouchListener { v, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    downX = event.x
                    downY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    val offsetX = event.x - downX
                    val offsetY = event.y - downY
                    offsetLeftAndRight(offsetX.toInt())
                    offsetTopAndBottom(offsetY.toInt())
                }
            }
            true
        }
    }
}

3.通过改变LayoutParams

这里有个坑,注意你的父布局。

比如约束布局上下左右的约束都设置了,那这个会有异常。上下都设置了约束,那view会居中,margin会在居中的基础上再生效。

class CustomView : View {
    var downX = 0f
    var downY = 0f

    constructor(context: Context?) : this(context, null, 0)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        setOnTouchListener { v, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    downX = event.x
                    downY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    val offsetX = event.x - downX
                    val offsetY = event.y - downY
                    val layoutParams = layoutParams as MarginLayoutParams
                    layoutParams.leftMargin = (left + offsetX).toInt()
                    layoutParams.topMargin = (top + offsetY).toInt()
                    setLayoutParams(layoutParams)
                }
            }
            true
        }
    }
}

4.scollTo与scollBy

scollTo(x,y)表示移动到一个具体的坐标点,而scollBy(dx,dy)则表示移动的增量为dx、dy。其中scollBy最终也是要调用scollTo的。scollTo、scollBy移动的是View的内容,如果在ViewGroup中使用则是移动他所有的子View。

class CustomView : View {
    var downX = 0f
    var downY = 0f

    constructor(context: Context?) : this(context, null, 0)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    init {
        setOnTouchListener { v, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    downX = event.x
                    downY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    val offsetX = event.x - downX
                    val offsetY = event.y - downY
                    (parent as View).scrollBy(-offsetX.toInt(), -offsetY.toInt())
                }
            }
            true
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值