グローバル座標(スクリーン座標)の取得


viewのローカル座標を取得するにはview.getTop()等を使用しますが、viewのスクリーン上での座標を取得したい場合などは、グローバル座標(スクリーン座標)を取得する必要があります。
Rect(四角形)のグローバル座標を取得するには、以下のように記述します。

Rect rect = new Rect();  
view.getGlobalVisibleRect(rect);

取得した座標を使用する時は、以下のように記述します。

//viewのtop取得
rect.top()
//viewのleft取得
rect.left()
//viewのright取得
rect.right()
//viewのbottom取得
rect.bottom()

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>