最简单实现悬窗按钮

package com.example.hellokotlin.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.animation.LinearInterpolator
import android.widget.LinearLayout
import com.example.hellokotlin.uitils.ExtensionCommon.deviceHeight
import com.example.hellokotlin.uitils.ExtensionCommon.deviceWidth


class ViewMoveFloatVip @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    LinearLayout(context, attrs, defStyleAttr) {

    private var downX = 0f
    private var downY = 0f
    private var isMove = false
    private val scrollThresholdValue = 20
    private val deviceHeight by lazy {
        context.deviceHeight()
    }
    private val deviceWidth by lazy {
        context.deviceWidth()
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        when (event.action and MotionEvent.ACTION_MASK) {
            MotionEvent.ACTION_DOWN -> {
                isMove = false
                downX = event.rawX
                downY = event.rawY
            }

            MotionEvent.ACTION_MOVE -> {
                val deltaX = event.rawX - downX
                val deltaY = event.rawY - downY
                if(deltaX > scrollThresholdValue || deltaY > scrollThresholdValue){
                    isMove = true
                    downX = event.rawX
                    downY = event.rawY
                }
            }
        }

        return isMove || super.onInterceptTouchEvent(event)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action and MotionEvent.ACTION_MASK) {
            MotionEvent.ACTION_DOWN -> {
                downX = event.rawX
                downY = event.rawY
            }

            MotionEvent.ACTION_MOVE -> {
                val deltaX = event.rawX - downX
                val deltaY = event.rawY - downY
                updateViewLocation(deltaX,deltaY)
                downX = event.rawX
                downY = event.rawY
            }

            MotionEvent.ACTION_UP -> {
                attachSideAnimation()
            }

        }
        return true
    }

    //不足屏幕一半,左。否则右
    private fun attachSideAnimation(){
        if(x < deviceWidth / 2f){
            animate()
                .setInterpolator(LinearInterpolator())
                .setDuration(500)
                .x(0f)
                .start()
        }else{
            animate()
                .setInterpolator(LinearInterpolator())
                .setDuration(500)
                .x(deviceWidth.toFloat() - width)
                .start()
        }
    }

    private fun updateViewLocation(deltaX: Float, deltaY: Float) {
//        val params = layoutParams
//        if(params is MarginLayoutParams){
//            params.topMargin = (params.topMargin + deltaY).toInt()
//            params.marginStart = (params.marginStart + deltaX).toInt()
//        }
//        layoutParams = params
        x += deltaX
        y += deltaY
    }

}

布局代码:


        

你可能感兴趣的:(自定义View,android)