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()
    }
}
相关推荐
海水冷却2 小时前
Android 群组聊天怎么实现:基于 ZIM 的完整落地指南(附可运行 Demo)
android
三少爷的鞋5 小时前
现代 Android 架构指南:不要再主动设计 Callback 接口
android
TimeFine10 小时前
AI 编码工作流的设计
android·ai编程
骑着蜗牛撵大象32711 小时前
C++20 协程 × 屏障:手写一个 co_await 版 Barrier,让等待不再钉死线程
android·c++20·协程·并发编程·co_await·屏障同步
山东孙燕姿13 小时前
设备台账重构:从一张表到三层模型
android·java·网络·低代码·重构·rxjava
蒸鱼Yuzheng14 小时前
移动端 CPU 调度分析:线程亲和性、大小核迁移与卡顿
android·perfetto·性能分析·线程优化·cpu调度
booboosui14 小时前
安卓唤醒词:12 维 MFCC 加 DTW,阈值 1.8
android
传奇开心果14 小时前
Compose Multiplatform 共享边界实战:架构契约、互操作同步与可回退设计
android·kotlin
事圆则缓14 小时前
从开机到首帧:Android 系统与应用启动流程源码分析
android
AI吃大瓜14 小时前
指针压力表读数4:Android实现指针压力表读数(可实时检测)
android·指针读数·指针压力表·压力表·压力表读数