getLocationOnScreen ,計算該視圖在全域座標系中的x,y值,(注意這個值是要從螢幕頂端算起,也就是索包括了通知欄的高度)//擷取在當前螢幕內的絕對座標
getLocationInWindow ,計算該視圖在它所在的widnow的座標x,y值,//擷取在整個視窗內的絕對座標 (不是很理解= =、)
getLeft , getTop, getBottom,
getRight, 這一組是擷取相對在它父親裡的座標
如果在Activity的OnCreate()事件輸出那些參數,是全為0,要等UI控制項都載入完了才能擷取到這些。
package xiaosi.location;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.ImageView;</p><p>public class LocationActivity extends Activity {<br /> /** Called when the activity is first created. */<br />private ImageView t = null;<br />private Button button = null;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> t = (ImageView)findViewById(R.id.l);<br /> button = (Button)findViewById(R.id.button);<br /> button.setOnClickListener(new buttonListener());<br /> }<br /> public class buttonListener implements OnClickListener{</p><p>public void onClick(View v)<br />{<br />int[] location = new int[2];<br /> t.getLocationOnScreen(location);<br /> int x = location[0];<br /> int y = location[1];<br /> System.out.println("x:"+x+"y:"+y);<br /> System.out.println("圖片各個角Left:"+t.getLeft()+"Right:"+t.getRight()+"Top:"+t.getTop()+"Bottom:"+t.getBottom());<br />}<br /> }<br />}
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical" ></p><p> <Button<br /> android:id="@+id/button"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="button"/><br /> <ImageView<br /> android:id="@+id/l"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:layout_gravity="center"<br /> android:src="@drawable/a" /><br /></LinearLayout>