Android 仿去哪儿携程地址互换效果

项目有用到一个类似于携程地址互换位置的 本来想去找找轮子 结果看了一圈没有一个能用的 还是自己手敲一个 

话不多说  看图

Android 仿去哪儿携程地址互换效果_第1张图片

我是kotlin写的 先贴下代码

布局文件的

 

  

                        

                            

                            
                        


                        

                            

                            
                        


                        

                            
                        


                    
   //声明的变量也贴下 害怕有些铁子不知道是干嘛的   
 var startAnimator: ObjectAnimator? = null
    var endAnimator: ObjectAnimator? = null

  constrainLayoutSwitch.setOnClickListener {
            constrainLayoutSwitch.isClickable = false //点击先将按钮不能点击
            var animatorSet: AnimatorSet? = null
            var iconAnimator: ObjectAnimator = ObjectAnimator.ofFloat(
                viewDetailStartEnd,
                View.ROTATION,
                viewDetailStartEnd.rotation,
                viewDetailStartEnd.rotation + 180
            )
            var leftMargin = 0
            var rightMargin = 0
            var leftLayoutParams: ConstraintLayout.LayoutParams? =
                tvDetailStartCity.layoutParams as ConstraintLayout.LayoutParams?
            leftMargin = leftLayoutParams!!.leftMargin

            var rightLayoutParams: ConstraintLayout.LayoutParams? =
                tvDetailEndCity.layoutParams as ConstraintLayout.LayoutParams?
            rightMargin = rightLayoutParams!!.rightMargin

            if (constrainLeft!!.translationX == 0f) {
                startAnimator =
                    ObjectAnimator.ofFloat(
                        constrainLeft,
                        View.TRANSLATION_X,
                        constrainLeft!!.translationX,
                        constrainRight!!.x + (constrainRight!!.width - constrainLeft!!.width) - leftMargin
                    )
                endAnimator = ObjectAnimator.ofFloat(
                    constrainRight,
                    View.TRANSLATION_X,
                    constrainRight!!.translationX,
                    -constrainRight!!.x + rightMargin
                )

            } else {
                startAnimator = ObjectAnimator.ofFloat(
                    constrainLeft,
                    View.TRANSLATION_X,
                    -constrainRight!!.translationX,
                    -constrainRight!!.x + leftMargin
                )

                endAnimator = ObjectAnimator.ofFloat(
                    constrainRight,
                    View.TRANSLATION_X,
                    constrainRight!!.translationX,
                    constrainRight!!.x - rightMargin
                )
                constrainLeft!!.translationX = 0f
            }
            animatorSet = AnimatorSet()
            animatorSet.addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator?) {
                }

                override fun onAnimationEnd(animation: Animator?) {
                    constrainLayoutSwitch.isClickable = true//动画结束 按钮可点击
                }

                override fun onAnimationCancel(animation: Animator?) {
                }

                override fun onAnimationRepeat(animation: Animator?) {
                }
            })
            animatorSet.playTogether(startAnimator, endAnimator, iconAnimator)
            animatorSet.duration = 500
            animatorSet.start()
        }

到这里就差不多完成了 分享下 有大佬的还得多多指点

后边有时间我会用java 写下也也贴上来

下载地址 https://github.com/903531306/AddressMaster 

我用java写一遍 害怕有些小伙伴kotlin 看不懂  下班下班

 

你可能感兴趣的:(Android,Android,Studio)