1

I want to animate a relative layout along the y-axis so I want to get its position on the y-axis. I tried many things like:

getY()
getTop()
getScreenLocation()

and so on, but everything is returning 0.

How can I find the position?

following is the code

ImageView advancetab = (ImageView) findViewById(R.id.imageView4);
RelativeLayout advancelayout = (RelativeLayout) findViewById(R.id.relativeLayout2);
final ObjectAnimator anim = ObjectAnimator.ofFloat(advancelayout, "Y", 
            advancelayout.getY(),advancelayout.getY()-100);
    anim.setDuration(1000);     
advancetab.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            anim.start();
        }
    });

When i clicked on the image then layout animate from 0,0 position to up.

3 Answers3

3

Here is my answer,

private int[] getViewLocations(View view) {
  int[] locations = new int[2];
  view.getLocationOnScreen(locations);
  return locations;
}

Use like this,

int[] locations = getViewLocations(yourcustomview);
int x = locations[0]; // x position of left
int y = locations[1]; // y position of top

Have fun.@.@

Luna Kong
  • 3,065
  • 25
  • 20
0

do write your code inside onwindowsfocuschange() of activity class...there you will get the parameters regarding your position on screen with getLocationonscreen() method of any view.

0

I think, I am not sure that you have to make a 'canvas' and then use the options to find the position of x and y coordinates.. May be there are other options, but I have done this using this only.

Stuti
  • 1,620
  • 1
  • 16
  • 33