2013-08-18 75 views
4

我想獲得屏幕中心的x和y座標。這裏是我的嘗試:如何在android中獲取屏幕的中心座標?

Display display = getWindowManager().getDefaultDisplay(); 
final Point size = new Point(); 
display.getSize(size); 
height = size.y; 
float centerY=height/2;//i expect this is the y coordinate of center 

但是,這並不工作,當我說:

SomeImageView.setY(centerY); 

我沒有看到它在屏幕的中央。誰能幫我這個?

感謝

回答

4

我猜你得到正確的中心座標,但setY是設置你的ImageView的底部邊緣的位置。關閉我的頭頂,你可以嘗試像

SomeImageView.setY(height - SomeImageView.getDrawable().getIntrinsicHeight()); 
+0

謝謝你,這工作,但它是設置在頂部邊緣,而我們並不需要劃分的內在高度鮑二,我指的是以下工作: SomeImageView.setY((height - SomeImageView.getDrawable()。getIntrinsicHeight())); – yrazlik

+0

是的,我通常需要一些試驗和錯誤,很高興你想出來:)(我正在糾正我的答案) – ssantos