Android: is there a way to get the last View position after TranslateAnimation?

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?

+5
source
3

, Android < 3.0, , , . , .. Android . getLocationOnScreen() 0. () , () . , Android.

+2

, , / , Transformation .

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) {
        }
    });

, Matrix.toString() . xTraveled yTraveled , TranslateAnimation ( , ).

+2

animations on gingerbread android and below do not really change the appearance in any way, just change the display method.

the only way to get a new position is to calculate it.

0
source

All Articles