下面列出了android.view.Display#getRealSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Return whether the navigation bar visible.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isSupportNavBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
WindowManager wm = (WindowManager) UtilsApp.getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return false;
Display display = wm.getDefaultDisplay();
Point size = new Point();
Point realSize = new Point();
display.getSize(size);
display.getRealSize(realSize);
return realSize.y != size.y || realSize.x != size.x;
}
boolean menu = ViewConfiguration.get(UtilsApp.getApp()).hasPermanentMenuKey();
boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
return !menu && !back;
}
/**
* 手机具有底部导航栏时,底部导航栏是否可见
*/
private static boolean isNavigationBarVisible(Activity activity) {
// View decorView = activity.getWindow().getDecorView();
// return (decorView.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 2;
boolean show = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Display display = activity.getWindow().getWindowManager().getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
View decorView = activity.getWindow().getDecorView();
Configuration conf = activity.getResources().getConfiguration();
if (Configuration.ORIENTATION_LANDSCAPE == conf.orientation) {
View contentView = decorView.findViewById(android.R.id.content);
if (contentView != null) {
show = (point.x != contentView.getWidth());
}
} else {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
show = (rect.bottom != point.y);
}
}
return show;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// get the default Display object representing the screen
Display display = ((WindowManager)
getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
Point screenSize = new Point(); // used to store screen size
display.getRealSize(screenSize); // store size in screenSize
// display the app's menu only in portrait orientation
if (screenSize.x < screenSize.y) // x is width, y is height
{
getMenuInflater().inflate(R.menu.main, menu); // inflate the menu
return true;
}
else
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceView = findViewById(R.id.surface_view);
mSurfaceView.getHolder().addCallback(this);
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSION_CAMERA_REQUEST_CODE);
} else {
isCheckPermissionOk = true;
}
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);
mSurfaceViewWidth = size.x;
mSurfaceViewHeight = size.y;
Log.e(TAG, "onCreate: " + size.x + "--" + size.y);
}
@SuppressLint({"NewApi"})
public static Point b() {
Display defaultDisplay = ((WindowManager) hn.a().c().getSystemService("window")).getDefaultDisplay();
Point point = new Point();
if (VERSION.SDK_INT >= 17) {
defaultDisplay.getRealSize(point);
} else if (VERSION.SDK_INT >= 14) {
try {
Method method = Display.class.getMethod("getRawHeight", new Class[0]);
point.x = ((Integer) Display.class.getMethod("getRawWidth", new Class[0]).invoke(defaultDisplay, new Object[0])).intValue();
point.y = ((Integer) method.invoke(defaultDisplay, new Object[0])).intValue();
} catch (Throwable th) {
defaultDisplay.getSize(point);
}
} else if (VERSION.SDK_INT >= 13) {
defaultDisplay.getSize(point);
} else {
point.x = defaultDisplay.getWidth();
point.y = defaultDisplay.getHeight();
}
return point;
}
/**
* 虚拟操作拦(home等)是否显示
*/
public static boolean isNavigationBarShow(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
Point realSize = new Point();
display.getSize(size);
display.getRealSize(realSize);
return realSize.y != size.y;
} else {
boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
if (menu || back) {
return false;
} else {
return true;
}
}
}
/**
* Returns the display size in dp (device-independent pixel)
*
* The returned display size is adjusted per screen rotation. Also this will return the actual
* size of the screen, rather than adjusted per system decorations (like status bar).
*
* @return a Point containing the display size in dp
*/
public Point getDisplaySizeDp() {
Tracer.trace();
Display display = getAutomatorBridge().getDefaultDisplay();
Point p = new Point();
display.getRealSize(p);
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
float dpx = p.x / metrics.density;
float dpy = p.y / metrics.density;
p.x = Math.round(dpx);
p.y = Math.round(dpy);
return p;
}
public boolean checkDeviceHasNavigationBar(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
Point realSize = new Point();
display.getSize(size);
display.getRealSize(realSize);
return realSize.y != size.y;
}
/**
* @param display The Display that the window being dragged is on.
*/
void register(Display display) {
display.getRealSize(mDisplaySize);
if (DEBUG_DRAG) Slog.d(TAG_WM, "registering drag input channel");
if (mInputInterceptor != null) {
Slog.e(TAG_WM, "Duplicate register of drag input channel");
} else {
mInputInterceptor = new InputInterceptor(display);
mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
}
}
public static int getWidth(Context context) {
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
Display display = manager.getDefaultDisplay();
display.getRealSize(size);
return size.x;
}
public static int getWidth(Context context) {
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
Display display = manager.getDefaultDisplay();
display.getRealSize(size);
return size.x;
}
public static boolean isNavigationBarShow(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
Point realSize = new Point();
display.getSize(size);
display.getRealSize(realSize);
return realSize.y != size.y;
} else {
boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
return !(menu || back);
}
}
private boolean isNavigationBarShow() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
Point realSize = new Point();
display.getSize(size);
display.getRealSize(realSize);
return realSize.y != size.y;
} else {
boolean menu = ViewConfiguration.get(this).hasPermanentMenuKey();
boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
return !(menu || back);
}
}
public static int getHeight(Context context) {
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
Display display = manager.getDefaultDisplay();
display.getRealSize(size);
return size.y;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
static DynamicGrid createDynamicGrid(Context context, DynamicGrid dynamicGrid) {
// Determine the dynamic grid properties
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point realSize = new Point();
display.getRealSize(realSize);
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
if (dynamicGrid == null) {
Point smallestSize = new Point();
Point largestSize = new Point();
display.getCurrentSizeRange(smallestSize, largestSize);
dynamicGrid = new DynamicGrid(context,
context.getResources(),
Math.min(smallestSize.x, smallestSize.y),
Math.min(largestSize.x, largestSize.y),
realSize.x, realSize.y,
dm.widthPixels, dm.heightPixels);
}
// Update the icon size
DeviceProfile grid = dynamicGrid.getDeviceProfile();
grid.updateFromConfiguration(context, context.getResources(),
realSize.x, realSize.y,
dm.widthPixels, dm.heightPixels);
return dynamicGrid;
}
/**
* Retrieves the real size of the default display
*
* @return The display size in 'WxH' format
*/
public String getRealDisplaySize() {
Display display = UiAutomatorBridge.getInstance().getDefaultDisplay();
android.graphics.Point p = new android.graphics.Point();
display.getRealSize(p);
return String.format("%sx%s", p.x, p.y);
}
/**
* Determines whether the current device is a natural portrait-oriented device
*
* <p>
* Using the current app's window to determine whether the device is a natural
* portrait-oriented device doesn't work in all scenarios, one example of this is multi-window
* mode.
* Taking a natural portrait-oriented device in multi-window mode, rotating it 90 degrees (so
* that it's in landscape), with the app open, and its window's width being smaller than its
* height. Using the app's width and height would determine that the device isn't
* naturally portrait-oriented, where in fact it is, which is why it is important to use the
* size of the device instead.
* </p>
*
* @param context Current context. Can be an {@link android.app.Application} context
* or an {@link android.app.Activity} context.
* @param rotationDegrees The device's rotation in degrees from its natural orientation.
* @return Whether the device is naturally portrait-oriented.
*/
private static boolean isNaturalPortrait(@NonNull final Context context,
final int rotationDegrees) {
final WindowManager windowManager = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
if (windowManager == null) {
return true;
}
final Display display = windowManager.getDefaultDisplay();
final Point deviceSize = new Point();
display.getRealSize(deviceSize);
final int width = deviceSize.x;
final int height = deviceSize.y;
return ((rotationDegrees == 0 || rotationDegrees == 180) && width < height) || (
(rotationDegrees == 90 || rotationDegrees == 270) && width >= height);
}
@TargetApi(17)
private static void getDisplaySizeV17(Display display, Point outSize) {
display.getRealSize(outSize);
}
@TargetApi(17)
private static void getDisplaySizeV17(Display display, Point outSize) {
display.getRealSize(outSize);
}
public static Bitmap takeScreenshot(int rotation, int screenWidth,
int screenHeight) {
Display display = DisplayManagerGlobal.getInstance().getRealDisplay(
Display.DEFAULT_DISPLAY);
Point displaySize = new Point();
display.getRealSize(displaySize);
final int displayWidth = screenWidth;
final int displayHeight = screenHeight;
final float screenshotWidth;
final float screenshotHeight;
switch (rotation) {
case UiAutomation.ROTATION_FREEZE_0: {
screenshotWidth = displayWidth;
screenshotHeight = displayHeight;
}
break;
case UiAutomation.ROTATION_FREEZE_90: {
screenshotWidth = displayHeight;
screenshotHeight = displayWidth;
}
break;
case UiAutomation.ROTATION_FREEZE_180: {
screenshotWidth = displayWidth;
screenshotHeight = displayHeight;
}
break;
case UiAutomation.ROTATION_FREEZE_270: {
screenshotWidth = displayHeight;
screenshotHeight = displayWidth;
}
break;
default: {
return null;
}
}
Bitmap screenShot = null;
try {
screenShot = SurfaceControl.screenshot((int) screenshotWidth,
(int) screenshotHeight);
if (screenShot == null) {
return null;
}
} catch (Exception re) {
return null;
}
if (rotation != UiAutomation.ROTATION_FREEZE_0) {
Bitmap unrotatedScreenShot = Bitmap.createBitmap(displayWidth,
displayHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(unrotatedScreenShot);
canvas.translate(unrotatedScreenShot.getWidth() / 2,
unrotatedScreenShot.getHeight() / 2);
canvas.rotate(getDegreesForRotation(rotation));
canvas.translate(-screenshotWidth / 2, -screenshotHeight / 2);
canvas.drawBitmap(screenShot, 0, 0, null);
canvas.setBitmap(null);
screenShot.recycle();
screenShot = unrotatedScreenShot;
}
// Optimization
screenShot.setHasAlpha(false);
return screenShot;
}