Android得到控制項在螢幕中的座標

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   color   

 

getLocationOnScreen ,計算該視圖在全域座標系中的x,y值,(注意這個值是要從螢幕頂端算起,也就是索包括了通知欄的高度)//擷取在當前螢幕內的絕對座標 

getLocationInWindow ,計算該視圖在它所在的widnow的座標x,y值,//擷取在整個視窗內的絕對座標 (不是很理解= =、)

getLeft , getTopgetBottomgetRight, 這一組是擷取相對在它父親裡的座標

如果在Activity的OnCreate()事件輸出那些參數,是全為0,要等UI控制項都載入完了才能擷取到這些。

[java] view plaincopy
 package xiaosi.location;    import android.app.Activity;  import android.os.Bundle;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  import android.widget.ImageView;    public class LocationActivity extends Activity {      /** Called when the activity is first created. */      private ImageView t = null;      private Button button = null;      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);                    t = (ImageView)findViewById(R.id.l);          button = (Button)findViewById(R.id.button);          button.setOnClickListener(new buttonListener());      }         public class buttonListener implements OnClickListener{            public void onClick(View v)          {              int[] location = new int[2];              t.getLocationOnScreen(location);              int x = location[0];              int y = location[1];              System.out.println("x:"+x+"y:"+y);              System.out.println("圖片各個角Left:"+t.getLeft()+"Right:"+t.getRight()+"Top:"+t.getTop()+"Bottom:"+t.getBottom());          }      }  }  

  

  view plaincopy

 
<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical" >        <Button          android:id="@+id/button"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="button"/>      <ImageView          android:id="@+id/l"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="center"          android:src="@drawable/a" />  </LinearLayout>  

 


 

 

http://blog.csdn.net/sjf0115/article/details/7306284

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.