Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Is there any way to get the latest position of View after TranslateAnimation?

I know multiple ways to get location values of a View.

getLocationOnScreen()
getLocationInWindow()
getLeft()

However, none of them actually returns the current location of the View I moved by startAnimation() method, but only the original location.

So, now let's make a View that moves to the right by 10 pixels on each Click (I'm omitting the layout, since you can just place whatever view in your main XML and give it onClickListener).

public class AndroidTestActivity extends Activity implements OnClickListener {  
LinearLayout testView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    testView = (LinearLayout) this.findViewById(R.id.test);
    testView.setOnClickListener(this);
}

public void onClick(View v) {
    int[] pLoS = new int[2];
    testView.getLocationOnScreen(pLoS);
    TranslateAnimation move = new TranslateAnimation(pLoS[0], pLoS[0] + 10, 0f, 0f);
    move.setFillAfter(true);
    move.setFillEnabled(true);
    testView.startAnimation(move);
}

}

As you see, this doesn't work as I intended, since getLocationOnScreen() always returns the same value (in my case, 0), and doen't reflect the value I used in TranslateAnimation...

Any idea?

like image 295
Quv Avatar asked Oct 07 '22 12:10

Quv


1 Answers

Assuming you're using Android < 3.0 then your question may be in a similar vein to mine I asked here. Basically Animations are separate from the View itself i.e. Android animates a copy of your View. That is why getLocationOnScreen() always returns 0. It's not the view that has moved (animated) it was the copy that moved (animated). If you see the answers to my question this issue has been addressed in later versions of Android.

like image 130
D-Dᴙum Avatar answered Oct 12 '22 00:10

D-Dᴙum