I know several ways to get view location values.
getLocationOnScreen()
getLocationInWindow()
getLeft()
However, none of them actually returns the current View I location moved by the startAnimation () method, but only the original location.
So, now let's make a view that moves 10 pixels to the right on each click (I omit the layout, as you can just place any 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 can see, this does not work as I expected, since getLocationOnScreen () always returns the same value (in my case, 0) and does not reflect the value that I used in TranslateAnimation ...
Any idea?
source