下面列出了android.view.Display#getRotation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
/* package */ void updateFromDisplay(Display display) {
Point size = new Point();
DisplayMetrics displayMetrics = new DisplayMetrics();
PixelFormat pixelFormat = new PixelFormat();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealSize(size);
display.getRealMetrics(displayMetrics);
} else {
display.getSize(size);
display.getMetrics(displayMetrics);
}
if (hasForcedDIPScale()) displayMetrics.density = sForcedDIPScale.floatValue();
// JellyBean MR1 and later always uses RGBA_8888.
int pixelFormatId = (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
? display.getPixelFormat()
: PixelFormat.RGBA_8888;
PixelFormat.getPixelFormatInfo(pixelFormatId, pixelFormat);
super.update(size, displayMetrics.density, pixelFormat.bitsPerPixel,
bitsPerComponent(pixelFormatId), display.getRotation());
}
/**
* Return the application orientation
* @return the application orientation of a Integer
*/
private int getApplicationOrientationAsNumber() {
final Display display = ((WindowManager) getReactApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch (display.getRotation()) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 1;
case Surface.ROTATION_180:
return 2;
case Surface.ROTATION_270:
return 3;
}
return 0;
}
private int getDisplayInfo(Point size) {
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
if (size != null) {
display.getSize(size);
}
int rotation = display.getRotation();
switch (rotation) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
}
throw new RuntimeException("Unknown screen rotation: " + rotation);
}
public void setControlMargins()
{
Activity activity = getActivity();
if (activity != null)
{
// get the margins accounting for the navigation and status bars
Display display = activity.getWindowManager().getDefaultDisplay();
float scale = getContext().getResources().getDisplayMetrics().density;
int margin = (int)(5 * scale + 0.5f);
int extra = Utils.getNavigationBarWidth(getContext());
int rotation = display.getRotation();
int leftMargin = margin;
int rightMargin = margin;
if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)
{
leftMargin += extra;
}
else
{
rightMargin += extra;
}
int topMargin = margin + Utils.getStatusBarHeight(getContext());
// set the control margins
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)closeButton.getLayoutParams();
lp.setMargins(leftMargin, topMargin, rightMargin, margin);
lp = (ViewGroup.MarginLayoutParams)snapshotButton.getLayoutParams();
lp.setMargins(leftMargin, margin, rightMargin, margin);
lp = (ViewGroup.MarginLayoutParams)nameView.getLayoutParams();
lp.setMargins(leftMargin, margin, rightMargin, margin);
}
}
/**
* 画面の回転状態を取得
* @param view
* @return Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180, Surface.ROTATION_270のいずれか
*/
@SuppressLint("NewApi")
@Rotation
public static int getRotation(@NonNull final View view) {
int rotation;
if (BuildCheck.isAPI17()) {
rotation = view.getDisplay().getRotation();
} else {
final Display display
= ContextUtils.requireSystemService(view.getContext(), WindowManager.class)
.getDefaultDisplay();
rotation = display.getRotation();
}
return rotation;
}
private void checkOrientation() {
WindowManager wm = getWindowManager();
if (wm == null) return;
Display display = wm.getDefaultDisplay();
if (display == null) return;
int oldOrientation = mCurrentOrientation;
mCurrentOrientation = display.getRotation();
if (oldOrientation != mCurrentOrientation) onOrientationChange(mCurrentOrientation);
}
private void setLayoutAspectRatio(Size cameraPreviewSize) {
AspectFrameLayout layout = findViewById(R.id.cameraPreview_afl);
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
mCameraPreviewWidth = cameraPreviewSize.getWidth();
mCameraPreviewHeight = cameraPreviewSize.getHeight();
if (display.getRotation() == Surface.ROTATION_0) {
layout.setAspectRatio((double) mCameraPreviewHeight / mCameraPreviewWidth);
} else if (display.getRotation() == Surface.ROTATION_180) {
layout.setAspectRatio((double) mCameraPreviewHeight / mCameraPreviewWidth);
} else {
layout.setAspectRatio((double) mCameraPreviewWidth / mCameraPreviewHeight);
}
}
public void setOrientation(Display display) {
CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = display.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
orientation = (info.orientation + degrees) % 360;
orientation = (360 - orientation) % 360; // compensate the mirror
} else { // back-facing
orientation = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(orientation);
}
/**
* Checks to see if the default Display in the WindowManager is rotated horizontally.
*
* @return true if the display is currently rotated horizontally, false otherwise.
*/
private static
boolean isDisplayHorizontal()
{
Display display = s_windowManager.getDefaultDisplay();
int rotation = display.getRotation();
return Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation;
}
private void initializeRecordingUI() {
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
mVideoRecorder = new VideoRecorder(this, mCaptureConfiguration, mVideoFile, new CameraWrapper(new NativeCamera(), display.getRotation()),
mVideoCaptureView.getPreviewSurfaceHolder());
mVideoCaptureView.setRecordingButtonInterface(this);
boolean showTimer = this.getIntent().getBooleanExtra(EXTRA_SHOW_TIMER, false);
mVideoCaptureView.showTimer(showTimer);
if (mVideoRecorded) {
mVideoCaptureView.updateUIRecordingFinished(getVideoThumbnail());
} else {
mVideoCaptureView.updateUINotRecording();
}
mVideoCaptureView.showTimer(mCaptureConfiguration.getShowTimer());
}
private void setParameters(Camera.Parameters params) {
boolean isDesiredSizeFound = false;
for (Camera.Size size : params.getSupportedPreviewSizes()) {
if (size.width == desiredWidth && size.height == desiredHeight) {
params.setPreviewSize(desiredWidth, desiredHeight);
isDesiredSizeFound = true;
}
}
if (!isDesiredSizeFound) {
Camera.Size ppsfv = params.getPreferredPreviewSizeForVideo();
if (ppsfv != null) {
params.setPreviewSize(ppsfv.width, ppsfv.height);
}
}
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
params.setRecordingHint(true);
camera.setParameters(params);
int[] fpsRange = new int[2];
params.getPreviewFpsRange(fpsRange);
Display display =
((WindowManager) context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_0) {
camera.setDisplayOrientation(90);
} else if (display.getRotation() == Surface.ROTATION_270) {
camera.setDisplayOrientation(180);
}
}
public int getDisplayOrientation() {
if (mCameraWrapper == null) {
//If we don't have a camera set there is no orientation so return dummy value
return 0;
}
Camera.CameraInfo info = new Camera.CameraInfo();
if(mCameraWrapper.mCameraId == -1) {
Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
} else {
Camera.getCameraInfo(mCameraWrapper.mCameraId, info);
}
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int rotation = display.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
return result;
}
@SuppressWarnings("depecation")
protected static int getRotation(Display mDisplay) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)
return mDisplay.getRotation();
else
return mDisplay.getOrientation();
}
private void listing16_14() {
float[] values = new float[3];
float[] inR = new float[9];
float[] outR = new float[9];
SensorManager.getRotationMatrix(inR, null,
mAccelerometerValues,
mMagneticFieldValues);
// Listing 16-14: Remapping the orientation reference frame based on the natural orientation of the device
// Determine the current orientation relative to the natural orientation
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int rotation = display.getRotation();
int x_axis = SensorManager.AXIS_X;
int y_axis = SensorManager.AXIS_Y;
switch (rotation) {
case (Surface.ROTATION_0): break;
case (Surface.ROTATION_90):
x_axis = SensorManager.AXIS_Y;
y_axis = SensorManager.AXIS_MINUS_X;
break;
case (Surface.ROTATION_180):
y_axis = SensorManager.AXIS_MINUS_Y;
break;
case (Surface.ROTATION_270):
x_axis = SensorManager.AXIS_MINUS_Y;
y_axis = SensorManager.AXIS_X;
break;
default: break;
}
SensorManager.remapCoordinateSystem(inR, x_axis, y_axis, outR);
// Obtain the new, remapped, orientation values.
SensorManager.getOrientation(outR, values);
}
/**
* this method is private, because we do not need to handle tablet
*
* @param context
* @return
*/
private static int getScreenRotation(Context context) {
WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (w == null) {
return Surface.ROTATION_0;
}
Display display = w.getDefaultDisplay();
if (display == null) {
return Surface.ROTATION_0;
}
return display.getRotation();
}
void setDesiredCameraParameters(Camera camera, boolean safeMode) {
// Checkout screen orientation
int rotation = context.getApplicationContext().getResources().getConfiguration().orientation;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
int deviceSpecificRotation = display.getRotation();
if (rotation == Configuration.ORIENTATION_PORTRAIT) {
if (deviceSpecificRotation == Surface.ROTATION_0 || deviceSpecificRotation == Surface.ROTATION_90) {
camera.setDisplayOrientation(90);
} else {
camera.setDisplayOrientation(270);
}
} else {
// landscape
if (deviceSpecificRotation == Surface.ROTATION_180 || deviceSpecificRotation == Surface.ROTATION_270) {
camera.setDisplayOrientation(180);
}
}
Camera.Parameters parameters = camera.getParameters();
if (parameters == null) {
Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
return;
}
Log.i(TAG, "Initial camera parameters: " + parameters.flatten());
if (safeMode) {
Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
initializeTorch(parameters, prefs, safeMode);
String focusMode = null;
if (prefs.getBoolean(PreferencesActivity.KEY_AUTO_FOCUS, true)) {
if (safeMode || prefs.getBoolean(PreferencesActivity.KEY_DISABLE_CONTINUOUS_FOCUS, false)) {
focusMode = findSettableValue(parameters.getSupportedFocusModes(),
Camera.Parameters.FOCUS_MODE_AUTO);
} else {
focusMode = findSettableValue(parameters.getSupportedFocusModes(),
"continuous-picture", // Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE in 4.0+
"continuous-video", // Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO in 4.0+
Camera.Parameters.FOCUS_MODE_AUTO);
}
}
// Maybe selected auto-focus but not available, so fall through here:
if (!safeMode && focusMode == null) {
focusMode = findSettableValue(parameters.getSupportedFocusModes(),
Camera.Parameters.FOCUS_MODE_MACRO,
"edof"); // Camera.Parameters.FOCUS_MODE_EDOF in 2.2+
}
if (focusMode != null) {
parameters.setFocusMode(focusMode);
}
parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
camera.setParameters(parameters);
}
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void lockOrientation(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
int height;
int width;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
height = display.getHeight();
width = display.getWidth();
} else {
Point size = new Point();
display.getSize(size);
height = size.y;
width = size.x;
}
switch (rotation) {
case Surface.ROTATION_90:
if (width > height)
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_180:
if (height > width)
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
else
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_270:
if (width > height)
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
else
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
default:
if (height > width)
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
else
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
@Override
public void onPreviewSizeChosen(final Size size, final int rotation) {
final float textSizePx = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE_DIP, getResources().getDisplayMetrics());
borderedText = new BorderedText(textSizePx);
borderedText.setTypeface(Typeface.MONOSPACE);
classifier =
TensorFlowImageClassifier.create(
getAssets(),
MODEL_FILE,
LABEL_FILE,
INPUT_SIZE,
IMAGE_MEAN,
IMAGE_STD,
INPUT_NAME,
OUTPUT_NAME);
previewWidth = size.getWidth();
previewHeight = size.getHeight();
final Display display = getWindowManager().getDefaultDisplay();
final int screenOrientation = display.getRotation();
LOGGER.i("Sensor orientation: %d, Screen orientation: %d", rotation, screenOrientation);
sensorOrientation = rotation + screenOrientation;
LOGGER.i("Initializing at size %dx%d", previewWidth, previewHeight);
rgbFrameBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
croppedBitmap = Bitmap.createBitmap(INPUT_SIZE, INPUT_SIZE, Config.ARGB_8888);
frameToCropTransform = ImageUtils.getTransformationMatrix(
previewWidth, previewHeight,
INPUT_SIZE, INPUT_SIZE,
sensorOrientation, MAINTAIN_ASPECT);
cropToFrameTransform = new Matrix();
frameToCropTransform.invert(cropToFrameTransform);
addCallback(
new DrawCallback() {
@Override
public void drawCallback(final Canvas canvas) {
renderDebug(canvas);
}
});
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static void setOrientation(Activity a, int orientation) {
boolean isKindleFireHD = false; // Fix an accelerometer bug with kindle fire HDs
String device = Settings.getSettings().deviceModel.toUpperCase(Locale.US);
String make = Settings.getSettings().deviceMake.toUpperCase(Locale.US);
if (make.equals("AMAZON")
&& (device.equals("KFTT") || device.equals("KFJWI") || device.equals("KFJWA"))) {
isKindleFireHD = true;
}
if ((a != null) && !a.isFinishing()) {
Display d = ((WindowManager) a.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = d.getRotation();
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
if (rotation == android.view.Surface.ROTATION_180) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
} else {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
if (isKindleFireHD) {
if (rotation == android.view.Surface.ROTATION_0 || rotation == android.view.Surface.ROTATION_90) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
} else {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} else {
if (rotation == android.view.Surface.ROTATION_0 || rotation == android.view.Surface.ROTATION_90) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
}
}
} //endif -- orientation is PORTRAIT; else LANDSCAPE
} //endif -- a && a
}
/**
* Get the current device orientation.
*
* @param context The context to get the resources and window service.
*
* @return The current activity orientation info.
*
* @see ActivityInfo#SCREEN_ORIENTATION_PORTRAIT
* @see ActivityInfo#SCREEN_ORIENTATION_REVERSE_PORTRAIT
* @see ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE
* @see ActivityInfo#SCREEN_ORIENTATION_REVERSE_LANDSCAPE
*/
public static int getScreenOrientation(@NonNull Context context) {
WindowManager windowManager = (WindowManager)
context.getSystemService(Context.WINDOW_SERVICE);
if (windowManager == null) {
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}
Display display = windowManager.getDefaultDisplay();
int rotation = display.getRotation();
DisplayMetrics displayMatrix = context.getResources().getDisplayMetrics();
float scale = displayMatrix.density;
int width = (int) (displayMatrix.widthPixels * scale + 0.5f);
int height = (int) (displayMatrix.heightPixels * scale + 0.5f);
int orientation;
if ((rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90
|| rotation == Surface.ROTATION_270) && width > height) {
switch (rotation) {
default:
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_180:
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
case Surface.ROTATION_270:
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
}
} else {
switch (rotation) {
default:
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_180:
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Surface.ROTATION_270:
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
}
}
return orientation;
}