5

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?

Quv
  • 2,543
  • 4
  • 30
  • 48

3 Answers3

2

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.

Community
  • 1
  • 1
D-Dᴙum
  • 7,278
  • 8
  • 50
  • 90
2

Well, if you are trying to see how many pixels the view has shifted during/after its animation, then you can open up the Transformation object on the animation.

Here's an example using the AnimationListener:

animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Transformation trans = new Transformation();
            // startTime + duration = end of animation
            int endTime = animation.getStartTime()+animation.getDuration();
            animation.getTransformation(endTime, trans);

            Matrix transformationMatrix = trans.getMatrix();
            float[] matrixVals = new float[9];
            transformationMatrix.getValues(matrixVals);
            float xTraveled = matrixVals[2];
            float yTraveled = matrixVals[5];
            // do something with them here...
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });

I may not be exactly correct in the array indices for these, definitely use Matrix.toString() and look at the values yourself. xTraveled and yTraveled will give you the amount of distance traveled by the TranslateAnimation at the indicated time (in this case, at the end of the animation).

lim
  • 261
  • 2
  • 6
0

animations on android gingerbread and below do not really change the view in any way , only change the way it is shown.

the only way to get the new position is by calculating it.

android developer
  • 106,412
  • 122
  • 641
  • 1,128