简体   繁体   English

我怎样才能获得我在Android中的视图中心x,y?

[英]How can i get center x,y of my view in android?

I am attaching an imageview up on my frame layout. 我在我的框架布局上添加了一个imageview。 Here i want to get my imageview center co-ordinates. 在这里,我想得到我的imageview中心坐标。 i will use that same co-ordinates to set my imageview in next layout. 我将使用相同的坐标在下一个布局中设置我的imageview。 Please suggest me how to get the center co-ordinates of my imageview. 请建议我如何获得我的imageview的中心坐标。

Thanks in advance 提前致谢

centre of the imageView will be imageView的中心将是

 centreX=imageView.getX() + imageView.getWidth()  / 2;
 centreY=imageView.getY() + imageView.getHeight() / 2;

but make sure you call it after the imageView created 但请确保在imageView创建后调用它

您可以使用getPivotX()getPivotY() ,它始终保持在视图中心甚至旋转视图。

Try this : 试试这个 :

ImageView my_image = (ImageView) findViewById(R.id.my_imageView);  

Rect rectf = new Rect();
my_image.getLocalVisibleRect(rectf);

Log.d("WIDTH        :",String.valueOf(rectf.width()));
Log.d("HEIGHT       :",String.valueOf(rectf.height()));
Log.d("left         :",String.valueOf(rectf.left));
Log.d("right        :",String.valueOf(rectf.right));
Log.d("top          :",String.valueOf(rectf.top));
Log.d("bottom       :",String.valueOf(rectf.bottom));

Hope this helps you. 希望这对你有所帮助。

Thanks. 谢谢。

As @aiueoH mentioned, you can use getPivotX() and getPivotY() . 正如@aiueoH所提到的,你可以使用getPivotX() and getPivotY()

Try using this for finding coordinates of centre: 尝试使用它来查找中心坐标:

int centreX = view.getPivotX()/2;
int centreY = view.getPivotY()/2;

if you wish to align a view at the center of other view then you can use this - 如果你想在其他视图的中心对齐一个视图,那么你可以使用这个 -

float center_x = parent_view.getX() + parent_view.getWidth()/2 - child_view.getWidth()/2;

and then obviously using - 然后显然使用 -

child_view.setX(center_x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM