在Android上的Translate Animation中获取ImageView的实时坐标

时间:2015-02-16 03:53:59

标签: java android imageview

我有一个ImageView,设置为翻译动画。我想要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);

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

变量x和y不会实时更新,只能给出ImageView的原始静态坐标。

如何在ImageView运动时获取实时坐标?

2 个答案:

答案 0 :(得分:0)

你可以这样试试,

   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);
    int[] firstPosition = new int[2];
    myimage.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY);
        myimage.getLocationOnScreen(firstPosition);
        int r = myimage.getMeasuredWidth() + firstPosition[1];
        int rr = myimage.getMeasuredWidth() + firstPosition[0];

答案 1 :(得分:0)

int[] imageCordinates = new int[2];
                imgv1.getLocationOnScreen(imageCordinates);

                x = imageCordinates[0] + (imgv1.getWidth() / 2);
                y = imageCordinates[1] + (imgv1.getHeight() / 2);