1. 程式人生 > >Android得到控制元件在螢幕中的座標

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

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

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

getLeft , getTop, getBottom, getRight, 這一組是獲取相對在它父親裡的座標

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

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());
		}
    }
}
<?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>

相關推薦

Android+HTML 針對android一個控制元件TextView文字顏色、大小不同的解決方案

tvPrice.setText((Html.fromHtml("<font color= '#FF0000'>"+"¥" + "<big>"+"示例文字"+"</big&

Android得到控制元件螢幕座標

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

android獲得控制元件螢幕的絕對座標 getLocationInWindow 和 getLocationOnScreen

int[] location = new  int[2] ; view.getLocationInWindow(location); //獲取在當前視窗內的絕對座標 view.getLocationOnScreen(location);//獲取在整個螢幕內的絕對座標 l

android獲得控制元件螢幕的絕對座標

int[] location = new  int[2] ; view.getLocationInWindow(location); //獲取在當前視窗內的絕對座標 view.getLocationOnScreen(location);//獲取在整個螢幕內的絕對座標 loc

android UI控制元件之webview控制元件使用例項:載入網頁到webview

package com.vincent.org.networkapp; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; impo

Android 獲取控制元件座標位置

知識點很少,只是為了做個筆記 獲取一下控制元件,比如 button EditText……的座標位置 View v1 = (View) button1; View v2 = (View) editText1; int[] l1 = {0, 0}; int[] lv2 =

android在滑動控制元件過程動態設定view背景透明的問題

2、在網上找了資料才知道,在佈局中多個控制元件同時使用一個資源的時候,這些控制元件會共用一個狀態,例如ColorState,如果你改變了一個控制元件的狀態,其他的控制元件都會接收到相同的通知。這時我們可以使用mutate()方法使該控制元件狀態不定,這樣不定狀態的控制元件就不會共享自己的狀態了。

android控制元件放在螢幕任意位置

private void showHalf() { WindowManager wm = PintuGameAct.this.getWindowManager(); int width = wm.getDefaultDisplay(

android 判斷控制元件是否在螢幕範圍內 是否可見

專案開發中,不免會用到動畫。而動畫展示的展示效果,既是當控制元件在螢幕範圍內可見時,動畫才執行,關鍵點就是判斷每時刻控制元件是否可見: // 監聽螢幕滑動狀態-當控制元件可見時,執行動畫Point p

android關於控制元件setTag(key,Object)的設定的相關問題

setTag () 是 Android 的 View 類中很有用的一個方法,可以用它來給控制元件附加一些資訊,在很多場合下都得到妙用。 我們可以看到 setTag() 有兩個方法過載,set

幾種Android表格控制元件

一、LockTableView GitHub:https://github.com/RmondJone/LockTableView 有基本功能(顯示資料),有上拉下拉,有點選事件,但其它功能(如新增按鈕等)需要二次開發。   二、PanelList GitHub:https://

WindowsXamlHost:在 WPF 使用 UWP 控制元件控制元件

原文 WindowsXamlHost:在 WPF 中使用 UWP 控制元件庫中的控制元件 在 WindowsXamlHost:在 WPF 中使用 UWP 的控制元件(Windows Community Toolkit) 一文中,我們說到了在 WPF 中引入簡單的 UWP 控制元件以及相關

Android CardView控制元件

相關文章 Android Snackbar控制元件 Android FloatingActionButton控制元件 Android Toolbar控制元件 Android AppBarLayout控制元件 Android CollapsingToolbarLayout控制元件 Andr

Android ViewStub控制元件

前言 使用ViewStub可以實現介面的延遲載入。 1. ViewStub簡單應用 (1) 佈局檔案 layout為載入View,inflatedId為載入後View的id。 <ViewStub android:id="@+id/view_stub"

Android ViewPager控制元件

前言 ViewPager可以實現多個介面的左右滑動。 1. ViewPager簡單應用 (1) 佈局檔案 <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:

android高階控制元件介紹

1. 高階控制元件與低階控制元件區別 高階控制元件需要介面卡 而低階不用 2. 介面卡種類  陣列介面卡 ArrayAdapter         new ArrayAdapter<String&

Android學習(六)—— Android高階控制元件

Android高階控制元件 1、ProgressBar 進度條,在執行耗時操作時來顯示,可以提高軟體的友好型。 常用屬性 android:max:進度條的最大值 android:progress:進度條已完成進度值 android:progressDrawable:設定軌道對應的Draw

Android學習(五)—— Android初級控制元件

Android初級控制元件 從今天開始就對Android進行正式的學習,首先先來學習一下初級的控制元件 1、TextView  文字框,用於顯示文字,不可編輯。 常用屬性 android:autoLink   設定是否當文字為URL連結/email/

Android 基礎控制元件(二)

##7.ImageView ImageView直接繼承View,也是非常常用的一種檢視控制元件。 示例程式碼: <ImageView android:id="@+id/testImage" android:layout_width="100dp" a

android 基礎控制元件————TextView

TextView是View的直接子類。它是一個文字顯示控制元件,提供了基本的顯示文字的功能,並且是大部分UI控制元件的父類,因為大部分UI控制元件都需要展示資訊。 如果僅僅是展示文字,那麼TextView的作用就太小了,所以它還預定義了一些類似於HTML的標籤,通過這些標籤可以使TextView