如何获得布局相对于屏幕的位置
问题描述:
我想要沿y轴动画相对布局,因此我想要在y轴上获得它的位置。 我试过很多东西,如:如何获得布局相对于屏幕的位置
getY()
getTop()
getScreenLocation()
等,但一切都返回0。
怎样才能找到我的位置?
以下是代码
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();
}
});
当我点击图像上然后从布局位置0,0动画最多。
答
我想,我不知道你必须做一个'画布',然后使用选项来找到x和y坐标的位置。可能还有其他选项,但我已经使用这个只要。
答
也许您的解决方案是
View.getLocationOnScreen(int[] location)
答
做写里面onwindowsfocuschange代码活动类的()...有你会得到关于你在屏幕上的位置与参数getLocationonscreen()方法的任何视图。
答
这里是我的回答,
private int[] getViewLocations(View view) {
int[] locations = new int[2];
view.getLocationOnScreen(locations);
return locations;
}
使用这样,
int[] locations = getViewLocations(yourcustomview);
int x = locations[0]; // x position of left
int y = locations[1]; // y position of top
玩得开心。@。@