下面列出了android.app.UiAutomation#ROTATION_FREEZE_180 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Check if the device is in its natural orientation. This is determined by checking if the
* orientation is at 0 or 180 degrees.
* @return true if it is in natural orientation
* @since API Level 17
*/
public boolean isNaturalOrientation() {
Tracer.trace();
waitForIdle();
int ret = getAutomatorBridge().getRotation();
return ret == UiAutomation.ROTATION_FREEZE_0 ||
ret == UiAutomation.ROTATION_FREEZE_180;
}
public int getDisplayUnRotationWidth() {
int rotation = getDisplayRotation();
if (rotation == UiAutomation.ROTATION_FREEZE_0
|| rotation == UiAutomation.ROTATION_FREEZE_180) {
return getDisplayWidth();
} else {
return getDisplayHeight();
}
}
public int getDisplayUnRotationHeight() {
int rotation = getDisplayRotation();
if (rotation == UiAutomation.ROTATION_FREEZE_0
|| rotation == UiAutomation.ROTATION_FREEZE_180) {
return getDisplayHeight();
} else {
return getDisplayWidth();
}
}
/**
* Check if the device is in its natural orientation. This is determined by
* checking if the orientation is at 0 or 180 degrees.
*
* @return true if it is in natural orientation
* @since API Level 17
*/
public boolean isNaturalOrientation() {
Tracer.trace();
waitForIdle();
int ret = getAutomatorBridge().getRotation();
return ret == UiAutomation.ROTATION_FREEZE_0
|| ret == UiAutomation.ROTATION_FREEZE_180;
}
public static Point toOriginPoint(int x, int y) {
int rotation = UiDevice.getInstance().getRotation();
int width = UiDevice.getInstance().getDisplayWidth();
int height = UiDevice.getInstance().getDisplayHeight();
int rWidth = xmax() - xmin();
int rHeight = ymax() - ymin();
int tempx = 0;
int tempy = 0;
int tempWidth = 0;
int tempHeight = 0;
// 将xy旋转回来
switch (rotation) {
case UiAutomation.ROTATION_FREEZE_0:
break;
case UiAutomation.ROTATION_FREEZE_90:
// 原来的x = 现在的y,原来的y = 现在的宽度-现在的x
tempx = y;
tempy = width - x;
x = tempx;
y = tempy;
tempWidth = height;
tempHeight = width;
break;
case UiAutomation.ROTATION_FREEZE_180:
// 原来的x = 现在的宽度-现在的x,原来的y=现在的高度-现在的y
tempx = width - x;
tempy = height - y;
x = tempx;
y = tempy;
break;
case UiAutomation.ROTATION_FREEZE_270:
// 原来的x = 现在的高度-现在的y,原来的y=现在的x
tempx = height - y;
tempy = x;
x = tempx;
y = tempy;
tempWidth = height;
tempHeight = width;
width = tempWidth;
height = tempHeight;
break;
}
// 缩放到驱动坐标
x = (x * rWidth / width);
y = (y * rHeight / height);
return new Point(x, y);
}
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;
}