13

I have an image of a bullet in an ImageView that does Translate animation.

I need to show real time coordinates to show how far it is from target in real time.

ImageView myimage = (ImageView)findViewById(R.id.myimage);    
Animation animation = new TranslateAnimation(100, 200, 300, 400);
animation.setDuration(1000);
myimage.startAnimation(animation);
animation.setRepeatCount(Animation.INFINITE);

Is it possible to get real time x and y coordinates of the image while it is doing TranslateAnimation ?

And if its not possible using TranslateAnimation, is there any other way that gives real time coordinates of image while in motion ?

I tried -

int x = myimage.getLeft();
int y = myimage.getTop(); 

and

int[] firstPosition = new int[2];
myimage.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY);
myimage.getLocationOnScreen(firstPosition);
int x = firstPosition[0];
int y = firstPosition[1];

but in both the ways, its giving the initial static coordinate of the ImageView.

Gissipi_453
  • 1,250
  • 1
  • 25
  • 61
  • I don't know how to do it programmatically, but you can always use maths :) in this type of situations – Ispas Claudiu Feb 16 '15 at 08:58
  • Yes. using maths is an ultimate way, but I'm looking if there is any other way to do in android. Looking if android has any specific function for it or any workaround. – Gissipi_453 Feb 16 '15 at 09:03
  • exactly at what point of time do you need those co-ords and why? – Amrut Bidri Feb 16 '15 at 09:47
  • @AmrutBidri , An image of a bullet does Translate animation. I need to show real time coordinates to show how far is it from target in real time. I'm still learning android and this is just practice code. – Gissipi_453 Feb 16 '15 at 09:55
  • You've already asked this question here: http://stackoverflow.com/questions/28534300/getting-real-time-coordinates-of-imageview-while-it-is-in-translate-animation-on?rq=1 It would have been better if you had edited and placed the bounty on your original question. Editing a question brings it back to the top of the list on StackOverflow (not only that, but the bounty probably would have attracted more attention to it as well). In other words, the same aim would have been achieved, but without cluttering up StackOverflow with duplicates. – Stephan Branczyk Feb 23 '15 at 06:32
  • And that was a big mistake on my part. I thought that the original question was deleted. Any way that I can correct the mistake ? @StephanBranczyk – Gissipi_453 Feb 23 '15 at 06:44
  • I suppose you could try deleting the original question (assuming the system will let you), since I don't know how you could delete this current question since you already placed a bounty on it. – Stephan Branczyk Feb 23 '15 at 06:56
  • @Gissipi_453 if you got solution of this question then please help me at https://stackoverflow.com/questions/62246817/how-to-get-positon-in-translateanimation – Akash Attal Jun 07 '20 at 15:26

3 Answers3

8

Here's a complete example based on what user3249477 and Vikram said:

    final TextView positionTextView = (TextView)findViewById(R.id.positionTextView);
    ImageView myimage = (ImageView)findViewById(R.id.imageView);    

    ObjectAnimator translateXAnimation= ObjectAnimator.ofFloat(myimage, "translationX", 0f, 100f);
    ObjectAnimator translateYAnimation= ObjectAnimator.ofFloat(myimage, "translationY", 0f, 100f);     
    translateXAnimation.setRepeatCount(ValueAnimator.INFINITE);      
    translateYAnimation.setRepeatCount(ValueAnimator.INFINITE);

    AnimatorSet set = new AnimatorSet();
    set.setDuration(1000);
    set.playTogether(translateXAnimation, translateYAnimation);
    set.start();

    translateXAnimation.addUpdateListener(new AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            imageXPosition = (Float)animation.getAnimatedValue();
        }
    });

    translateYAnimation.addUpdateListener(new AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            imageYPosition = (Float)animation.getAnimatedValue();
            String position = String.format("X:%d Y:%d", (int)imageXPosition, (int)imageYPosition);
            positionTextView.setText(position);
        }
    });
Spiri
  • 1,283
  • 11
  • 24
  • 1
    Got the point about how its done. Thanks a lot. I'm one month old in coding android and looks like I still need to study a lot. – Gissipi_453 Feb 28 '15 at 13:25
  • I'm using TranslateAnimation on button click. so please can you u explain how i get current position of view after end of translate animation – Akash Attal Jun 07 '20 at 11:25
3

You can use an ObjectAnimator (API 11+):

ImageView iv = (ImageView)findViewById(R.id.markerRed);

// Create animators for x and y axes
ObjectAnimator oax = ObjectAnimator.ofFloat(iv, "translationX", 0f, 100f);
ObjectAnimator oay = ObjectAnimator.ofFloat(iv, "translationY", 0f, 100f);
oax.setRepeatCount(Animation.INFINITE);
oay.setRepeatCount(Animation.INFINITE);

// Combine Animators and start them together
AnimatorSet set = new AnimatorSet();
set.setDuration(1000);
set.playTogether(oax, oay);
set.start();

Then fetch the animation values like this:

Log.e("TAG", "X: " + oax.getAnimatedValue() + " Y:" + oay.getAnimatedValue());

If you add these values to the initial ImageView's coordinates, you'll get the current location.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • oax.getAnimatedValue() and oay.getAnimatedValue() still gives the initial coordinates only. not updating. – Gissipi_453 Feb 23 '15 at 09:22
  • @Gissipi_453 how and when do you call this? The animation must have begun and is in the middle of animating. – Simas Feb 23 '15 at 14:28
  • OP could use a `ValueAnimator` along with `PropertyValuesHolder` for `View.TRANSLATION_X` & `View.TRANSLATION_Y`. In case of a `ValueAnimator`, one can add an `AnimatorUpdateListener` using `addUpdateListener(...)`. – Vikram Feb 23 '15 at 21:49
3

I had this idea: you can extend the standard TranslateAnimation and intercept every step of the animation by overriding the applyTransformation method.

Take a look at this incomplete/untested snippet:

private class ObservableTranslateAnimation extends TranslateAnimation{

    private float matrixValues[] = new float[9];
    private float actualDx;
    private float actualDy;

    public ObservableTranslateAnimation(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // ... more constructors 

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        //After that super is called, the matrix gets updated!

        //get the current matrix and extract what you need
        t.getMatrix().getValues(matrixValues);
        actualDx = matrixValues[Matrix.MTRANS_X];
        actualDy = matrixValues[Matrix.MTRANS_Y];

        /*
          Notify someone here (a listener?), or just read the values of actualDx, actualDy from outside
          You can also play around with the other Matrix values.
        */
    }
}
bonnyz
  • 13,458
  • 5
  • 46
  • 70