Getting real time coordinates of ImageView while it is in Translate Animation on android

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-07 05:06:08

问题


I have an ImageView, set to Translate animation. I want real time coordinates of the ImageView when it is moving.

My code here:

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

Variables x and y don't updatee real time and only give original static coordinate of the ImageView.

How can I get the real time coordinates as the ImageView is in motion ?


回答1:


You can try like this,

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



回答2:


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

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


来源:https://stackoverflow.com/questions/28534300/getting-real-time-coordinates-of-imageview-while-it-is-in-translate-animation-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!