在翻译动画中是否可以获得ImageView的实时坐标?

时间:2015-02-16 08:55:07

标签: android imageview coordinates translate-animation

我在ImageView中有一张子弹的图像,用于翻译动画。

我需要显示实时坐标以显示它与目标的实时距离。

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

在进行TranslateAnimation时,是否可以获得图像的实时x和y坐标?

如果使用TranslateAnimation不可能,还有其他方法可以在运动时提供图像的实时坐标吗?

我试过了 -

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

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

但是在这两种方式中,它给出了ImageView的初始静态坐标。

3 个答案:

答案 0 :(得分:7)

以下是基于user3249477和Vikram所说的完整示例:

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

答案 1 :(得分:3)

您可以使用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();

然后像这样获取动画值:

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

如果您将这些值添加到初始ImageView坐标中,您将获得当前位置。

答案 2 :(得分:3)

我有这个想法:你可以扩展标准的TranslateAnimation ,并通过覆盖applyTransformation方法拦截动画的每一步。

看一下这个不完整/未经测试的片段:

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.
        */
    }
}