2011-06-13 77 views
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动画最多。

回答

0

我想,我不知道你必须做一个'画布',然后使用选项来找到x和y坐标的位置。可能还有其他选项,但我已经使用这个只要。

0

做写里面onwindowsfocuschange代码活动类的()...有你会得到关于你在屏幕上的位置与参数getLocationonscreen()方法的任何视图。

2

这里是我的回答,

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 

玩得开心。@。@

相关问题