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()
    }
}
相关推荐
一一Null几秒前
Access Token 和 Refresh Token 的双令牌机制,维持登陆状态
android·python·安全·flask
_祝你今天愉快13 分钟前
深入理解 Android Handler
android
pengyu1 小时前
【Flutter 状态管理 - 四】 | setState的工作机制探秘
android·flutter·dart
溪饱鱼2 小时前
DHgate爆火背后的技术原因
android·前端·ios
木子予彤2 小时前
Compose Side Effect(附带效应)
android·android jetpack
Tanecious.2 小时前
机器视觉--Python补充知识
android·开发语言·python
不是AI3 小时前
【安卓开发】【Android Studio】Menu(菜单栏)的使用及常见问题
android·ide·android studio
顾北川_野4 小时前
Android 不插SIM卡,手机不能拨打紧急电话;2g+gsm配置才支持112紧急拨号
android
百锦再5 小时前
Android Studio 中文字大小的单位详解
android·xml·java·ide·app·android studio
隐-梵5 小时前
Android studio前沿开发--利用socket服务器连接AI实现前后端交互(全站首发思路)
android·服务器·人工智能·后端·websocket·android studio·交互