【问题标题】:Is it possible to get real time coordinates of an ImageView while it is in Translate animation?是否可以在翻译动画中获取 ImageView 的实时坐标?
【发布时间】:2015-02-27 18:59:49
【问题描述】:

我在执行翻译动画的 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 的初始静态坐标。

【问题讨论】:

  • 我不知道如何以编程方式进行,但在这种情况下你总是可以使用数学 :)
  • 是的。使用数学是一种终极方式,但我正在寻找在 android 中是否有其他方法可以做。查看android是否有任何特定功能或任何解决方法。
  • 您究竟在什么时间点需要这些坐标,为什么?
  • @AmrutBidri ,子弹的图像会翻译动画。我需要显示实时坐标以实时显示它与目标的距离。我还在学习android,这只是练习代码。
  • 您已经在这里问过这个问题:stackoverflow.com/questions/28534300/… 如果您对原始问题进行了编辑并提供了赏金会更好。编辑一个问题会将它带回 StackOverflow 上的列表顶部(不仅如此,赏金可能也会吸引更多的关注)。换句话说,可以实现相同的目标,但不会用重复项使 StackOverflow 混乱。

标签: android imageview coordinates translate-animation


【解决方案1】:

这是一个基于 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);
        }
    });

【讨论】:

  • 明白它是如何完成的。非常感谢。我在 android 编码方面已经 1 个月大了,看来我还需要学习很多东西。
  • 没问题 - 很高兴它有帮助:)
  • 我在按钮点击时使用 TranslateAnimation。所以请你解释一下我如何在翻译动画结束后获得当前的视图位置
【解决方案2】:

您可以使用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 的坐标中,您将获得当前位置。

【讨论】:

  • oax.getAnimatedValue() 和 oay.getAnimatedValue() 仍然只给出初始坐标。没有更新。
  • @Gissipi_453 您如何以及何时调用它?动画必须已经开始并且正在制作动画。
  • OP 可以将ValueAnimatorPropertyValuesHolder 一起用于View.TRANSLATION_XView.TRANSLATION_Y。如果是ValueAnimator,可以使用addUpdateListener(...) 添加AnimatorUpdateListener
【解决方案3】:

我有这样的想法:您可以扩展标准的 TranslateAnimation 并通过覆盖 applyTransformation 方法来截取动画的每一步。

看看这个不完整/未经测试的sn-p:

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

【讨论】:

    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    相关资源
    最近更新 更多