[英]How can i get center x,y of my view in android?
我在我的框架布局上添加了一個imageview。 在這里,我想得到我的imageview中心坐標。 我將使用相同的坐標在下一個布局中設置我的imageview。 請建議我如何獲得我的imageview的中心坐標。
提前致謝
imageView的中心將是
centreX=imageView.getX() + imageView.getWidth() / 2;
centreY=imageView.getY() + imageView.getHeight() / 2;
但請確保在imageView創建后調用它
您可以使用getPivotX()
和getPivotY()
,它始終保持在視圖中心甚至旋轉視圖。
試試這個 :
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));
希望這對你有所幫助。
謝謝。
正如@aiueoH所提到的,你可以使用getPivotX() and getPivotY()
。
嘗試使用它來查找中心坐標:
int centreX = view.getPivotX()/2;
int centreY = view.getPivotY()/2;
如果你想在其他視圖的中心對齊一個視圖,那么你可以使用這個 -
float center_x = parent_view.getX() + parent_view.getWidth()/2 - child_view.getWidth()/2;
然后顯然使用 -
child_view.setX(center_x);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.