Android 如何获取布局相对于屏幕的位置
我想沿y轴设置相对布局的动画,以便获得其在y轴上的位置。 我尝试了很多东西,比如:Android 如何获取布局相对于屏幕的位置,android,android-animation,Android,Android Animation,我想沿y轴设置相对布局的动画,以便获得其在y轴上的位置。 我尝试了很多东西,比如: getY() getTop() getScreenLocation() 以此类推,但一切都返回0 我怎样才能找到这个职位 下面是代码 ImageView advancetab = (ImageView) findViewById(R.id.imageView4); RelativeLayout advancelayout = (RelativeLayout) findViewById(R.id.relative
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坐标的位置。。可能还有其他选项,但我只使用了此选项。请在activity类的WindowsFocusChange()中编写代码……在那里,您将使用任何视图的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
玩得开心@