Android 關(guān)于屏幕蜘醋、組件挂捅、父組件的位置關(guān)系

開篇一張圖

1,獲取屏幕寬高

/**
 * 獲得屏幕寬度
 *
 * @return
 */
public static int getScreenWidth() {
    WindowManager wm = (WindowManager) TTCommon.INSTANCE.getAppContext()
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.widthPixels;
}

/**
 * 獲得屏幕高度
 *
 * @return
 */
public static int getScreenHeight() {
    WindowManager wm = (WindowManager) TTCommon.INSTANCE.getAppContext()
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.heightPixels;
}

/**
 * 獲得屏幕高度 實(shí)際屏幕
 *
 * @return
 */
public static int getRealScreenHeight() {
    WindowManager wm = (WindowManager) TTCommon.INSTANCE.getAppContext()
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getRealMetrics(outMetrics);
    return outMetrics.heightPixels;
}

/**
 * 獲取用戶狀態(tài)欄的高度
 */
@SuppressLint("PrivateApi")
public static int getDeviceStatusHeight() {
    int statusHeight = -1;
    try {
        Class<?> clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        int height = Integer.parseInt(clazz.getField("status_bar_height")
                .get(object).toString());
        statusHeight = TTCommon.INSTANCE.getAppContext().getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusHeight;
}

2堂湖,獲取組件相對父控件的位置

通過給組件添加 addOnLayoutChangeListener 監(jiān)聽闲先,可通過繪制前后的參數(shù)變化,查看出組件相對于父控件的位置无蜂,單位都是px伺糠。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/vg_root_view"
    tools:context=".ViewGroupActivity">

    <Button
        android:id="@+id/button1"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="奔波霸"
        android:layout_marginStart="30dp" />

</RelativeLayout>
//...

button1.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        Log.d("TAG","oldTop="+oldTop);   // oldTop = 0
        Log.d("TAG","top="+top);         // top = 300
        Log.d("TAG","oldLeft="+oldLeft); // oldLeft = 0
        Log.d("TAG","left="+left);       // left = 90
    }
});

3,獲得點(diǎn)擊事件處 相對點(diǎn)擊控件 & 屏幕的坐標(biāo)

該方式是通過motionEvent獲取的

motionEvent event;

event.getX();       
event.getY();

event.getRawX();    
event.getRawY();

如圖:

4 , 獲取控件 相對 窗口Window 的位置

int[] location = new int[2];
view.getLocationInWindow(location);
int x = location[0]; // view距離window 左邊的距離(即x軸方向)
int y = location[1]; // view距離window 頂邊的距離(即y軸方向)

// 注:要在onWindowFocusChanged()里獲取斥季,即等window窗口發(fā)生變化后

如圖:

5, 獲得 View 相對 屏幕 的絕對坐標(biāo)

nt[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0]; // view距離 屏幕左邊的距離(即x軸方向)
int y = location[1]; // view距離 屏幕頂邊的距離(即y軸方向)

// 注:要在view.post(Runable)里獲取训桶,即等布局變化后

如圖:

6, View可見部分 相對于 屏幕的坐標(biāo)。

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

globalRect.getLeft();
globalRect.getRight();
globalRect.getTop();
globalRect.getBottom();

如圖:

7, View可見部分 相對于 自身View位置左上角的坐標(biāo)酣倾。

Rect localRect = new Rect();
view.getLocalVisibleRect(localRect);

localRect.getLeft();
localRect.getRight();
localRect.getTop();
localRect.getBottom();

如圖:

8舵揭,px 和 dip 單位換算

/**
 * px 轉(zhuǎn) dp
 * @param context
 * @param pxValue
 * @return
 */
public  int px2dip(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
}

/**
 * dp 轉(zhuǎn) px
 * @param context
 * @param dipValue
 * @return
 */
public static int dip2px(Context context, float dipValue){
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int)(dipValue * scale + 0.5f);
}

?

參考來自這哦

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市躁锡,隨后出現(xiàn)的幾起案子午绳,更是在濱河造成了極大的恐慌,老刑警劉巖映之,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拦焚,死亡現(xiàn)場離奇詭異,居然都是意外死亡杠输,警方通過查閱死者的電腦和手機(jī)赎败,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蠢甲,“玉大人僵刮,你說我怎么就攤上這事○信#” “怎么了搞糕?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長能岩。 經(jīng)常有香客問我寞宫,道長,這世上最難降的妖魔是什么拉鹃? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任辈赋,我火速辦了婚禮,結(jié)果婚禮上膏燕,老公的妹妹穿的比我還像新娘钥屈。我一直安慰自己,他們只是感情好坝辫,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布篷就。 她就那樣靜靜地躺著,像睡著了一般近忙。 火紅的嫁衣襯著肌膚如雪竭业。 梳的紋絲不亂的頭發(fā)上智润,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天,我揣著相機(jī)與錄音未辆,去河邊找鬼窟绷。 笑死,一個胖子當(dāng)著我的面吹牛咐柜,可吹牛的內(nèi)容都是我干的兼蜈。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼拙友,長吁一口氣:“原來是場噩夢啊……” “哼为狸!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起遗契,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤辐棒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后姊途,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體涉瘾,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年捷兰,在試婚紗的時候發(fā)現(xiàn)自己被綠了立叛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡贡茅,死狀恐怖秘蛇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情顶考,我是刑警寧澤赁还,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站驹沿,受9級特大地震影響艘策,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜渊季,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一朋蔫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧却汉,春花似錦驯妄、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春微猖,著一層夾襖步出監(jiān)牢的瞬間谈息,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工励两, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留黎茎,地道東北人。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓当悔,卻偏偏與公主長得像,于是被迫代替她去往敵國和親踢代。 傳聞我的和親對象是個殘疾皇子盲憎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內(nèi)容