android 屏幕屬性

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){
        //應用程序App區域寬高等尺寸獲取
        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){
        //View佈局區域寬高等尺寸獲取
        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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章