2023-07-14

2023-07-13  本文已影响0人  努力不负青春

    val line: ImageView
    val fp: FPGradientView
    val fp2: FPGradientView
    private var distance = 0f

    val animationSet1 = AnimationSet(true)
    val animationSet2 = AnimationSet(true)



    init {
        View.inflate(context, R.layout.layout_fingerprint, this)
        line = findViewById(R.id.line)
        fp = findViewById(R.id.fp)
        fp2 = findViewById(R.id.fp2)
        fp.isup = true
        fp2.isup = false

        line.startAnimation(animationSet1)
        fp.start()
        resetAnim()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        distance = h * 0.63f

        fp.setDistance(distance)
        fp.setRadius(h * 0.6f * 48 / 100)
        fp.setWidth(w / 1f)
        fp.setMHeight(h / 4f)
        fp.setNHeight(h / 1f)

        fp2.setDistance(distance)
        fp2.setRadius(h * 0.6f * 48 / 100)
        fp2.setWidth(w / 1f)
        fp2.setMHeight(h / 4f)
        fp2.setNHeight(h / 1f)

        val alphaAnimation1 = AlphaAnimation(0f, 1.0f)
        alphaAnimation1.duration = 500

        val alphaAnimation2 = AlphaAnimation(1.0f, 0f)
        alphaAnimation2.duration = 500
        alphaAnimation2.startOffset = 500


        val translateAnimation1 = TranslateAnimation(
            line.x, //x轴起始位置
            line.x, //x轴最终位置
            line.y, //y轴起始位置
            line.y + distance //y轴最终位置
        )
        translateAnimation1.duration = 1000 //移动持续1秒
        translateAnimation1.interpolator = LinearInterpolator()

        val translateAnimation2 = TranslateAnimation(
            line.x, //x轴起始位置
            line.x, //x轴最终位置
            line.y + distance, //y轴起始位置
            line.y  //y轴最终位置
        )
        translateAnimation2.duration = 1000 //移动持续1秒
        translateAnimation2.interpolator = LinearInterpolator()

        animationSet1.apply {
            addAnimation(alphaAnimation1)
            addAnimation(alphaAnimation2)
            addAnimation(translateAnimation1)
        }
        animationSet2.apply {
            addAnimation(alphaAnimation1)
            addAnimation(alphaAnimation2)
            addAnimation(translateAnimation2)
        }


        animationSet1.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                line.startAnimation(animationSet2)
                fp2.start()
            }

            override fun onAnimationRepeat(animation: Animation?) {}
        })

        animationSet2.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {}
            override fun onAnimationEnd(animation: Animation?) {
                line.startAnimation(animationSet1)
                fp.start()
            }

            override fun onAnimationRepeat(animation: Animation?) {}
        })
        Log.d("FPWidget", "start")

    }

    //销毁时停止动画
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        animationSet1.cancel()
        animationSet2.cancel()
    }

    //开始动画
    fun startAnim(){
        animationSet1.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                line.startAnimation(animationSet2)
                fp2.start()
            }

            override fun onAnimationRepeat(animation: Animation?) {}
        })

        animationSet2.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {}
            override fun onAnimationEnd(animation: Animation?) {
                line.startAnimation(animationSet1)
                fp.start()
            }

            override fun onAnimationRepeat(animation: Animation?) {}
        })
        line.alpha = 1f
        line.startAnimation(animationSet1)
        fp.start()
    }

    //停止动画
    fun resetAnim(){
        animationSet1.setAnimationListener(null)
        animationSet2.setAnimationListener(null)
        animationSet1.cancel()
        animationSet2.cancel()
        line.clearAnimation()
        line.alpha = 0f
        fp.cancel()
        fp2.cancel()
    }

    var job:Job? = null
    var clickTime = 0L
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event?.action){
            MotionEvent.ACTION_DOWN ->{
                Log.i("limengzhen","down")
            start()
            }
            MotionEvent.ACTION_UP -> {
                Log.i("limengzhen","up")
                end()
            }
        }
        return super.onTouchEvent(event)
    }

    private fun start(){
        clickTime = System.currentTimeMillis()

        job = GlobalScope.launch(Dispatchers.Main) {
            delay(500)
            startAnim()
        }
    }

    private fun end(){
        val diffTime = System.currentTimeMillis() - clickTime
        if (diffTime < 500) {
            job?.let {
                if (it.isActive){
                    it.cancel()
                }
            }
        } else if (diffTime > 500 && diffTime <4000){
            resetAnim()
        } else {
            Toast.makeText(context,"恭喜你完成一次事件。",Toast.LENGTH_LONG).show()
            resetAnim()
        }

    }



}
上一篇下一篇

猜你喜欢

热点阅读