Android 拖转改变视图高度

复制代码
class DraggableBottomView(
    context: Context,
    attrs: AttributeSet? = null,
) :LinearLayout(context, attrs, 0) {

    private var gestureDetector: GestureDetectorCompat
    private var initialY = 0f
    private var initialX = 0f
    private var initialHeight = 0
    private val minHeight: Int
    private val maxHeight: Int
    private val touchSlop: Int //触摸滑动距离
    private var isDragging = false // 是否正在拖动
    private var isUpOrDown = "up" // up向上  down 向下

    init {
        minHeight = CommonUtil.dpToPx(context,200)
        maxHeight = CommonUtil.dpToPx(context,500)
        touchSlop = ViewConfiguration.get(context).scaledTouchSlop
        gestureDetector = GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() {
            override fun onDown(e: MotionEvent): Boolean {
                initialY = e.rawY
                initialX = e.rawX
                initialHeight = height
                return true
            }
        })

    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        gestureDetector.onTouchEvent(event)
        when (event.action) {
            MotionEvent.ACTION_MOVE -> {
                val dy = event.rawY - initialY
                val dx = event.rawX - initialX
                if (Math.abs(dy) > touchSlop && Math.abs(dy)>Math.abs(dx)){
                    isUpOrDown = if (dy < 0f) "up" else "down"
                    isDragging = true
                }else{
                    isDragging = false
                }
                // 设置跟随手指滑动
                val newHeight = (initialHeight - dy).toInt()
                val clampedHeight = newHeight.coerceIn(minHeight, maxHeight)
                val params = layoutParams as ViewGroup.LayoutParams
                params.height = clampedHeight
                layoutParams = params
                requestLayout()

            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                if (isDragging) {
                    // 滑动松手后的逻辑
                    if (isUpOrDown == "up") {
                        animateHeightChange(maxHeight)
                    } else {
                        // 固定到初始位置位置
                        animateHeightChange(minHeight)
                    }
                    isDragging = false
                } else {

                }
            }
        }
        return true
    }

    private fun animateHeightChange(targetHeight: Int) {
        val valueAnimator = ValueAnimator.ofInt(height, targetHeight)
        valueAnimator.duration = 200 // 动画持续时间,可根据需要调整
        valueAnimator.addUpdateListener { animator ->
            val animatedValue = animator.animatedValue as Int
            val params = layoutParams as ViewGroup.LayoutParams
            params.height = animatedValue
            layoutParams = params
            requestLayout()
        }
        valueAnimator.start()
    }
}
相关推荐
Dwyane033 小时前
Android实战经验篇-AndroidScrcpyClient投屏一
android
_可乐无糖3 小时前
Appium 检查安装的驱动
android·ui·ios·appium·自动化
一名技术极客5 小时前
Python 进阶 - Excel 基本操作
android·python·excel
我是大佬的大佬6 小时前
在Android Studio中如何实现综合实验MP3播放器(保姆级教程)
android·ide·android studio
lichong9516 小时前
【Flutter&Dart】MVVM(Model-View-ViewModel)架构模式例子-http版本(30 /100)
android·flutter·http·架构·postman·win·smartapi
刘争Stanley6 小时前
Android系统开发(六):从Linux到Android:模块化开发,GKI内核的硬核科普
android·linux·运维·内核·镜像·gki·kmi
五味香6 小时前
Java学习,List截取
android·java·开发语言·python·学习·golang·kotlin
m0_748246616 小时前
Ubuntu22部署MySQL5.7详细教程
android·adb
xvch15 小时前
Kotlin 2.1.0 入门教程(三)
android·kotlin