本文实例为大家分享了android实现购票起始点位置交换的具体代码,供大家参考,具体内容如下
效果图:
点击交换位置按钮,北京和深圳布局交换位置。
xml布局文件:
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" match_parent " android: layout_height=" match_parent " android: fitsSystemWindows=" true " 面向android:="水平"比; & lt; TextView android: id=癅 + id/left_tv” android: layout_width=" 0 dp” android: layout_height=" wrap_content " android: layout_weight=" 1 " android:重力="中心" android:文本="北京”/比; & lt;按钮 android: id=癅 + id/btn” android: layout_width=" 0 dp” android: layout_height=" wrap_content " android: layout_weight=" 1 " android:重力="中心" android:文本="交换位置”/比; & lt; TextView android: id=癅 + id/right_tv” android: layout_width=" 0 dp” android: layout_height=" wrap_content " android: layout_weight=" 1 " android:重力="中心" android:文本="深圳”/比; & lt;/LinearLayout>
java代码:
公开课TESTButtonActivity延伸AppCompatActivity { 私人int startX; 私人int endX; 私人TextView leftCityTextView; 私人TextView rightCityTextView; 私人ValueAnimator endCityAnimator; 私人ValueAnimator startCityAnimation; @Override 保护无效onCreate (@Nullable包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)); 按钮mBtn=((按钮)this.findViewById (R.id.btn)); mBtn。setOnClickListener(新View.OnClickListener () { @Override 公共空间onClick(查看v) { startCityAnimation.start (); endCityAnimator.start (); } }); } 私人空间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 公共空间onWindowFocusChanged(布尔hasFocus) { super.onWindowFocusChanged (hasFocus); getLocation (); int leftMoveX=endX - startX; int rightMoveX=endX - startX; startCityAnimation=ValueAnimator。leftMoveX ofInt (0) .setDuration (5000); startCityAnimation。addUpdateListener(新ValueAnimator.AnimatorUpdateListener () { @Override 公共空间onAnimationUpdate (ValueAnimator动画){ int value=https://www.yisu.com/zixun/(int) animation.getAnimatedValue ();//重新布的局 leftCityTextView。布局(startX +价值, leftCityTextView.getTop (), startX +价值+ leftCityTextView.getWidth (), leftCityTextView.getBottom ()); } }); endCityAnimator=ValueAnimator。rightMoveX ofInt (0) .setDuration (5000); endCityAnimator。addUpdateListener(新ValueAnimator.AnimatorUpdateListener () { @Override 公共空间onAnimationUpdate (ValueAnimator动画){ int value=(int) animation.getAnimatedValue ();//重新布的局 rightCityTextView。布局(endX -值, rightCityTextView.getTop (), endX + rightCityTextView.getWidth()——的价值, rightCityTextView.getBottom ()); } }); endCityAnimator。addListener(新Animator.AnimatorListener () { @Override 公共空间onAnimationStart(动画动画){ } @Override 公共空间onAnimationEnd(动画动画){//用于下次交换 TextView tempTextView=leftCityTextView; leftCityTextView=rightCityTextView; rightCityTextView=tempTextView; } @Override 公共空间onAnimationCancel(动画动画){ } @Override 公共空间onAnimationRepeat(动画动画){ } }); } } >之前以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
android实现携程购票起始点位置交换