服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android实现携程购票起始点位置交换

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

2022-03-06 20:44Mars-xq Android

这篇文章主要为大家详细介绍了android实现携程购票起始点位置交换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android实现购票起始点位置交换的具体代码,供大家参考,具体内容如下

效果图:

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

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

xml布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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) {
      }
    });
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/sinat_31057219/article/details/80604223

延伸 · 阅读

精彩推荐