public class CoordinateUtils {
/**
* 状态栏+title+view
* @param context
* @return
*/
public static DisplayMetrics getScreenWH(Activity context){
DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics;
}
/**
* 去掉状态栏
* @param context
* @return
*/
public static Rect getAppWH(Activity context){
Rect rect = new Rect();
context.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
return rect;
}
/**
* 状态栏
* @param context
* @return
*/
public static int getStatusH(Activity context){
Rect rect = new Rect();
context.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
return statusBarHeight;
}
/**
* 去掉状态栏,去掉title
* @param context
* @return
*/
public static Rect getAppLayoutWH(Activity context){
Rect rect = new Rect();
context.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);
return rect;
}
}
public class CoordinateActivitity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* 0 0??? 不能在onCreate中調用
* 最好在Activity的onWindowFocusChanged ()方法或者之后调运,因为只有这时候才是真正的显示OK
*/
private void appLayout() {
Log.d("shj--", "appLayout width " + CoordinateUtils.getAppLayoutWH(this).width() + " height " + CoordinateUtils.getAppLayoutWH(this).height());
}
/**
* 0??? 不能在onCreate中調用
* 最好在Activity的onWindowFocusChanged ()方法或者之后调运,因为只有这时候才是真正的显示OK
*/
private void statusbar() {
Log.d("shj--", "statusBarHeight " + CoordinateUtils.getStatusH(this));
}
/**
* 跟screen一样 不能在onCreate中調用
* 最好在Activity的onWindowFocusChanged ()方法或者之后调运,因为只有这时候才是真正的显示OK
*/
private void app() {
Log.d("shj--", "app width " + CoordinateUtils.getAppLayoutWH(this).width() + " height " + CoordinateUtils.getAppLayoutWH(this).height());
}
/**
* 可以
*/
private void screen() {
Log.d("shj--", "screen widthPixels " + CoordinateUtils.getScreenWH(this).widthPixels + " heightPixels " + CoordinateUtils.getScreenWH(this).heightPixels);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
screen();
app();
statusbar();
appLayout();
}
}