0

I am making a game in which an imageView of a ball need to move from one point on the screen to another one. The starting point of the ball is on an imageView of a hole and I want it to end on another imageView of a hole.

Anyway, I didn't find any way to get the position of the starting point (the first hole) or the end point (the second hole). I tried to use getLocationOnScreen(Loc) but it returns 0 all the time.

I really need your help because I am looking for a solution for a long time. If someone can write a detailed answer that would work I will be grateful. Thanks in advance :)

Here is what I tried:

int fromLoc[] = new int[2];
holes[6].getLocationOnScreen(fromLoc);     
float startX = fromLoc[0];
float startY = fromLoc[1];

and this:

RelativeLayout.MarginLayoutParams params = 
    (RelativeLayout.MarginLayoutParams) imgBall0.getLayoutParams();
params.setMargins(holes[5].getLeft(), holes[5].getTop(), 
    holes[5].getRight(), holes[5].getBottom()); //all in pixels
imgBall0.setLayoutParams(params);

holes[5] is the ending point to which the imgBall0 needs to arrive. holes[5] is an imageButton which is in a relativeLayout and in a LinearLayout.

Thanks again!

nitz123
  • 27
  • 7

2 Answers2

0

Have you tried getX() and getY() methods? They return an int representing view position in pixels.

StG
  • 257
  • 2
  • 11
  • Be aware of the actual pixel coordinates of the screen though: http://stackoverflow.com/questions/11483345/how-do-android-screen-coordinates-work – StG Aug 07 '15 at 08:36
0

If you want to make a game with smooth animation,I advice to use a fullscreen SurfaceView or View and draw the ball and hole with canvas. It is not suitable to make a game by using android Widget such as ImageView.

Money
  • 346
  • 2
  • 3