问题
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