1

我想为沿 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 位置向上。

4

3 回答 3

3

这是我的答案,

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

玩得开心。@。@

于 2015-06-02T08:30:37.857 回答
0

请在活动类的 onwindowsfocuschange() 中编写代码...在那里,您将使用任何视图的 getLocationonscreen() 方法获得有关您在屏幕上位置的参数。

于 2013-11-11T09:45:08.227 回答
0

我想,我不确定您是否必须制作“画布”,然后使用选项来查找 x 和 y 坐标的位置。可能还有其他选项,但我仅使用此选项。

于 2011-06-13T06:13:34.367 回答