android實現(xiàn)攜程購票起始點位置交換
本文實例為大家分享了android實現(xiàn)購票起始點位置交換的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

點擊交換位置按鈕,北京和深圳布局交換位置。
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);//獲取坐標(biāo)
int[] endXLocation = new int[2];
rightCityTextView.getLocationOnScreen(endXLocation);
startX = startXLocation[0];//0為x坐標(biāo)
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) {
}
});
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用Activity實現(xiàn)從底部彈出菜單或窗口的方法
這篇文章主要介紹了Android使用Activity實現(xiàn)從底部彈出菜單或窗口的方法,涉及Android布局、窗口、事件監(jiān)聽、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Android?妙用TextView實現(xiàn)左邊文字,右邊圖片
這篇文章主要介紹了Android?妙用TextView實現(xiàn)左邊文字,右邊圖片的相關(guān)資料,需要的朋友可以參考下2023-07-07
Android RecyclerView item選中放大被遮擋問題詳解
這篇文章主要介紹了Android RecyclerView item選中放大被遮擋問題詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Android RadioGroup多行顯示效果 解決單選問題
這篇文章主要為大家詳細(xì)介紹了Android RadioGroup多行顯示效果,解決單選問題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
android實現(xiàn)可自由移動、監(jiān)聽點擊事件的懸浮窗
這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)可自由移動、監(jiān)聽點擊事件的懸浮窗,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android 使用Vibrator服務(wù)實現(xiàn)點擊按鈕帶有震動效果
這篇文章主要介紹了Android 使用Vibrator服務(wù)實現(xiàn)點擊按鈕帶有震動效果,,本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)火鍋工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06

