android 携程买票的起始点交换位置实现

本文介绍了一个简单的Android应用程序,该程序通过点击按钮实现两个文本视图的位置交换动画效果。使用ValueAnimator来平滑地改变视图的位置,并通过更新布局实现动画过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:

这里写图片描述

点击交换位置按钮,北京和深圳布局交换位置。

xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/left_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="北京" />

    <Button
        android:id="@+id/btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="交换位置" />

    <TextView
        android:id="@+id/right_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="深圳" />

</LinearLayout>

java代码:

public class TESTButtonActivity extends AppCompatActivity {
    private int startX;
    private int endX;
    private TextView leftCityTextView;
    private TextView rightCityTextView;
    private ValueAnimator endCityAnimator;
    private ValueAnimator startCityAnimation;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);

        leftCityTextView = ((TextView) this.findViewById(R.id.left_tv));
        rightCityTextView = ((TextView) this.findViewById(R.id.right_tv));

        Button mBtn = ((Button) this.findViewById(R.id.btn));
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startCityAnimation.start();
                endCityAnimator.start();
            }
        });
    }

    private void getLocation() {
        int[] startXLocation = new int[2];
        leftCityTextView.getLocationOnScreen(startXLocation);//获取坐标
        int[] endXLocation = new int[2];
        rightCityTextView.getLocationOnScreen(endXLocation);
        startX = startXLocation[0];//0为x坐标
        endX = endXLocation[0];
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        getLocation();

        int leftMoveX = endX - startX;
        int rightMoveX = endX - startX;

        startCityAnimation = ValueAnimator.ofInt(0, leftMoveX).setDuration(5000);
        startCityAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                //重新布局
                leftCityTextView.layout(startX + value,
                        leftCityTextView.getTop(),
                        startX + value + leftCityTextView.getWidth(),
                        leftCityTextView.getBottom());
            }
        });

        endCityAnimator = ValueAnimator.ofInt(0, rightMoveX).setDuration(5000);
        endCityAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                //重新布局
                rightCityTextView.layout(endX - value,
                        rightCityTextView.getTop(),
                        endX + rightCityTextView.getWidth() - value,
                        rightCityTextView.getBottom());
            }
        });

        endCityAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //用于下次交换
                TextView tempTextView = leftCityTextView;
                leftCityTextView = rightCityTextView;
                rightCityTextView = tempTextView;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值