Android 关于屏幕、组件、父组件的位置关系

开篇一张图

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;
}

/**
 * 获得屏幕高度 实际屏幕
 *
 * @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;
}

/**
 * 获取用户状态栏的高度
 */
@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 监听,可通过绘制前后的参数变化,查看出组件相对于父控件的位置,单位都是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,获得点击事件处 相对点击控件 & 屏幕的坐标

该方式是通过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窗口发生变化后

如图:

5, 获得 View 相对 屏幕 的绝对坐标

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可见部分 相对于 屏幕的坐标。

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

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

如图:

7, View可见部分 相对于 自身View位置左上角的坐标。

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

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

如图:

8,px 和 dip 单位换算

/**
 * px 转 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 转 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);
}

参考来自这哦

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容