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

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

话不多说  看图

我是kotlin写的 先贴下代码

布局文件的

 

  <androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/constrainCenter"
                        android:layout_width="match_parent"
                        android:layout_height="91dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="21dp"
                        android:layout_marginEnd="16dp"
                        android:background="@drawable/shape_medical_rescue"
                        app:layout_constraintTop_toTopOf="parent">

                        <androidx.constraintlayout.widget.ConstraintLayout
                            android:id="@+id/constrainLayoutLeft"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content">

                            <TextView
                                android:id="@+id/tvDetailStartCity"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="16dp"
                                android:layout_marginTop="16dp"
                                android:gravity="left"
                                android:text="深圳"
                                android:textColor="@color/text_color_black"
                                android:textSize="26sp"
                                android:textStyle="bold"
                                app:layout_constraintLeft_toLeftOf="parent"
                                app:layout_constraintTop_toTopOf="parent" />

                            <TextView
                                android:id="@+id/tvDetailStartName"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="16dp"
                                android:gravity="left"
                                android:text="宝安基地"
                                android:textColor="@color/base_color"
                                android:textSize="12sp"
                                app:layout_constraintLeft_toLeftOf="parent"
                                app:layout_constraintTop_toBottomOf="@id/tvDetailStartCity" />
                        </androidx.constraintlayout.widget.ConstraintLayout>


                        <androidx.constraintlayout.widget.ConstraintLayout
                            android:id="@+id/constrainLayoutRight"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="16dp"
                            app:layout_constraintBottom_toBottomOf="@id/constrainLayoutLeft"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toTopOf="@id/constrainLayoutLeft">

                            <TextView
                                android:id="@+id/tvDetailEndCity"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"

                                android:layout_marginEnd="16dp"
                                android:gravity="left"
                                android:text="珠海"
                                android:textColor="@color/text_color_black"
                                android:textSize="26sp"
                                android:textStyle="bold"

                                app:layout_constraintTop_toTopOf="parent" />

                            <TextView
                                android:id="@+id/tvDetailEndName"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginEnd="16dp"
                                android:gravity="left"
                                android:text="珠海基地"
                                android:textColor="@color/base_color"
                                android:textSize="12sp"
                                app:layout_constraintRight_toRightOf="parent"
                                app:layout_constraintTop_toBottomOf="@id/tvDetailEndCity" />
                        </androidx.constraintlayout.widget.ConstraintLayout>


                        <androidx.constraintlayout.widget.ConstraintLayout
                            android:id="@+id/constrainLayoutSwitch"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:padding="10dp"
                            app:layout_constraintBottom_toBottomOf="@id/constrainLayoutLeft"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toTopOf="@id/constrainLayoutRight">

                            <TextView
                                android:id="@+id/viewDetailStartEnd"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@mipmap/ic_change_direction"


                                />
                        </androidx.constraintlayout.widget.ConstraintLayout>


                    </androidx.constraintlayout.widget.ConstraintLayout>
   //声明的变量也贴下 害怕有些铁子不知道是干嘛的   
 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 看不懂  下班下班

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我想吃辣条

觉得不错,就可怜可怜博主吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值