上滑取消view

此view顯示界面是一個橫條,可在上滑指定距離後自動滑動至隱藏。或者上滑距離不夠則自動回退至原始位置,支持設置點擊跳轉監聽

SlideTopView.ktandroid

import android.animation.Animator
import android.animation.ObjectAnimator
import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.MotionEvent
import com.koolearn.android.utils.Log
import com.koolearn.android.utils.Utils

class SlideTopView(context: Context,attr: AttributeSet):ConstraintLayout(context,attr){
//    private var lastX = 0
    private var lastY = 0;
    var animation:ObjectAnimator?=null
    var downTime:Long?=null
    private lateinit var onSlideTopListener:()->Unit
    private lateinit var onClickListener:()->Unit

    override fun onTouchEvent(event: MotionEvent): Boolean {
//        val x = event.rawX.toInt()
        val y = event.rawY.toInt()

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
//                lastX = x
                lastY = y
                downTime = System.currentTimeMillis()
            }
            MotionEvent.ACTION_MOVE -> {
//                val offsetX = x - lastX
                var offsetY = y - lastY
                if(top+offsetY>0){//只能向上滑動
                    offsetY=0
                }
                layout(left,top + offsetY,right,bottom + offsetY)
//                onSlideTopListener(top+offsetY)
//                lastX = x
                lastY = y
            }
            MotionEvent.ACTION_UP -> {
                Log.i("share","top=$top")
                if(top>-height+ Utils.dp2px(15f)){
                    startAnim(0)
                    Log.i("share","移到0")
                }else{
                    startAnim(-height)
                    Log.i("share","移到-height")
                }
                if(downTime!=null&&System.currentTimeMillis()-downTime!!<500){
                    //0是取消顯示 1是點擊跳轉
                    onClickListener()
                }
            }
        }
        return true
    }

    /**
     * 設置動畫移動偏移量
     */
    fun startAnim(offSet:Int){
        animation = ObjectAnimator.ofInt(this,"offset",top,offSet)
        var i=top-offSet.toLong()
        animation?.duration = if(i>0) i else -i
        animation?.addListener(object:Animator.AnimatorListener{
            override fun onAnimationRepeat(animation: Animator?) {
            }

            override fun onAnimationEnd(animation: Animator?) {
                if(offSet!=0){
                    //0是取消顯示 1是點擊跳轉
                    onSlideTopListener()
                }
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationStart(animation: Animator?) {
            }

        })
        animation?.start()
    }

    fun setOffset(offset: Int) {
        Log.i("share","offset=$offset")
        layout(left,offset,right,offset+height)
    }

    fun setOnSlideTop(onSlideTopListener:()->Unit){
        this.onSlideTopListener = onSlideTopListener;
    }

    fun setOnClickListener(onClickListener:()->Unit){
        this.onClickListener = onClickListener;
    }
}
複製代碼
相關文章
相關標籤/搜索