開篇一張圖
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);
}
?