Is It Possible To Get Real Time Coordinates Of An Imageview While It Is In Translate Animation?
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 =
Solution 1:
Here's a complete example based on what user3249477 and Vikram said:
finalTextViewpositionTextView= (TextView)findViewById(R.id.positionTextView);
    ImageViewmyimage= (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);
    AnimatorSetset=newAnimatorSet();
    set.setDuration(1000);
    set.playTogether(translateXAnimation, translateYAnimation);
    set.start();
    translateXAnimation.addUpdateListener(newAnimatorUpdateListener() {
        @OverridepublicvoidonAnimationUpdate(ValueAnimator animation) {
            imageXPosition = (Float)animation.getAnimatedValue();
        }
    });
    translateYAnimation.addUpdateListener(newAnimatorUpdateListener() {
        @OverridepublicvoidonAnimationUpdate(ValueAnimator animation) {
            imageYPosition = (Float)animation.getAnimatedValue();
            Stringposition= String.format("X:%d Y:%d", (int)imageXPosition, (int)imageYPosition);
            positionTextView.setText(position);
        }
    });
Solution 2:
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 = newAnimatorSet();
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.
Solution 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:
privateclassObservableTranslateAnimationextendsTranslateAnimation{
    privatefloat matrixValues[] = newfloat[9];
    privatefloat actualDx;
    privatefloat actualDy;
    publicObservableTranslateAnimation(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    // ... more constructors @OverrideprotectedvoidapplyTransformation(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.
        */
    }
}
Post a Comment for "Is It Possible To Get Real Time Coordinates Of An Imageview While It Is In Translate Animation?"