开发者

How to get the position of a Layout relative to screen

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) {
      开发者_StackOverflow社区      // TODO Auto-generated method stub
            anim.start();
        }
    });

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


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.@.@


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜