1

I have a test code using the View Property Animation to switch locations of two buttons. The code is as below:

public class TestAnimationActivity extends Activity {
  private View mainView;     
  private View TempView;
  private Button B1, B2, B3;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate your view
    setContentView(R.layout.main_test);

    mainView = findViewById(R.id.mainTest);
    B1 = (Button) mainView.findViewById(R.id.B1);
    B2 = (Button) mainView.findViewById(R.id.B2);
    B3 = (Button) mainView.findViewById(R.id.B3);              

    OnClickListener OCL = new OnClickListener(){
        @Override
        public void onClick(View v) {               

            if(TempView == null){
                TempView = v;
            }else{

                int originalPos1[] = new int[2];
                TempView.getLocationOnScreen( originalPos1 );

                int originalPos2[] = new int[2];
                v.getLocationOnScreen( originalPos2 );


                v.animate().translationX((float)originalPos1[0] - (float)originalPos2[0]);
                v.animate().translationY((float)originalPos1[1] - (float)originalPos2[1]);

                TempView.animate().translationX((float)originalPos2[0] - (float)originalPos1[0]);
                TempView.animate().translationY((float)originalPos2[1] - (float)originalPos1[1]);

                TempView = null;                                                                                
            }                           
        }};     

    B1.setOnClickListener(OCL);
    B2.setOnClickListener(OCL);
    B3.setOnClickListener(OCL);

  } 
}

The XML file is as below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainTest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/B1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#B4B4B4"
    android:gravity="center" />

<Button
    android:id="@+id/B2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#470047"
    android:gravity="center" />

<Button
    android:id="@+id/B3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:gravity="center" />

</LinearLayout>

When I was testing this code on my phone. The animation did not work properly.

Two buttons will switch position the first time, but the button will go to wrong position if I continue clicking them. It seems that the button still calculate the position it should move to based on its original location when the app launched.

Anyone knows what is going wrong?

Jerry Qiao
  • 21
  • 4
  • I think this may occur because of the status bar. Calculate the Top offset could solve this problem. Refer to this: http://stackoverflow.com/questions/2638342/incorrect-coordinates-from-getlocationonscreen-getlocationinwindow – Jerry Qiao Sep 19 '13 at 18:14

0 Answers0