Aparna
Aparna

Reputation: 845

In android How to Translate to an absolute Point in Screen

I have an Imageview and an ImageButton and want the Imageview to move to the imagebutton's position.

The Imageview and its location on screen

myAnimation1.getLocationInWindow(imagePos);

The Image button and its location on screen

ImageButton.getLocationInWindow(buttonPos);

To translate or move the Imageview to the Imagebutton I tried

Animation move = new TranslateAnimation(Animation.ABSOLUTE,imagePos[0],Animation.ABSOLUTE,buttonPos[0],Animation.ABSOLUTE,imagePos[1],Animation.ABSOLUTE,buttonPos[1]);

But it is not working. The translations are not seen on screen. Any help would be appreciated a lot!! Thankyou in advance.

Upvotes: 0

Views: 1073

Answers (2)

Bernardo So
Bernardo So

Reputation: 11

First, just in case you are not getting the right coordinates from getLocationInWindow, try getting the coordinates like this:

ImageButton.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            public void onGlobalLayout() {

              ImageButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);

              int[] locations = new int[2];
              ImageButton.getLocationOnScreen(locations);
              // x is locations[0];
              // y is locations[1];

            }
        });

Upvotes: 1

Praveena
Praveena

Reputation: 6941

You did not set the duration and not even started the animation.

move.setDuration(1000);
imageView.startAnimation(move);

And then try to play with the attributes of animation to get the desired result

Upvotes: 0

Related Questions