下面列出了android.content.pm.ActivityInfo#SCREEN_ORIENTATION_PORTRAIT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onResume() {
super.onResume();
// set the screen to portrait
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
// if the bluetooth adatper is not support and enabled
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
finish();
}
// request to open the bluetooth adapter
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
private void setOrientation(String orientation) {
// Set correct orientation
int orientationConst;
switch (orientation) {
case "portrait":
orientationConst = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case "landscape":
orientationConst = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case "reverseLandscape":
orientationConst = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case "unspecified":
default:
orientationConst = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
break;
}
setRequestedOrientation(orientationConst);
}
@Override
public void onHideCustomView() {
LogUtils.i("Info", "onHideCustomView:" + moiveView);
if (moiveView == null)
return;
if (mActivity!=null&&mActivity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
moiveView.setVisibility(View.GONE);
if (moiveParentView != null && moiveView != null) {
moiveParentView.removeView(moiveView);
}
if (moiveParentView != null)
moiveParentView.setVisibility(View.GONE);
if(this.mCallback!=null)
mCallback.onCustomViewHidden();
this.moiveView = null;
if (mWebView != null)
mWebView.setVisibility(View.VISIBLE);
}
private int getCurrentOrientation() {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
switch (rotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_90:
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
default:
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
switch (rotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_270:
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
default:
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 设置为竖屏
*/
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
spf = getSharedPreferences(SPF_Name, Context.MODE_PRIVATE);
MessageCenter.getInstance(this);
// 初始化
setProgressDialog();
}
/**
* Locks the device window in actual screen mode
*/
public static void lockOrientation(Activity activity) {
Display display = ((WindowManager) activity.
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
switch (activity.getResources().getConfiguration().orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Configuration.ORIENTATION_PORTRAIT:
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
//noinspection ResourceType
activity.setRequestedOrientation(orientation);
}
@Override
public void onSensorChanged(SensorEvent event) {
try {
if (event.sensor == mRotationSensor && getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
if (event.values.length > 4) {
float[] truncatedRotationVector = new float[4];
System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
update(truncatedRotationVector);
} else {
update(event.values);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private int getCurrentOrientation() {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
switch (rotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_90:
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
default:
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
switch (rotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_270:
return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
default:
return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
}
private void releaseVideoView() {
mVideoView.release();
if (mVideoView.isFullScreen()) {
mVideoView.stopFullScreen();
}
if(getActivity().getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
mCurPos = -1;
}
private void createPreview() {
surfaceView = new GLSurfaceView(getApplicationContext());
((RelativeLayout)findViewById(R.id.streamer_layout)).addView(surfaceView, 0);
preview = capture.createPreview(surfaceView, camera);
if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
capture.setOrientation(90);
} else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ){
capture.setOrientation(0);
}
preview.start();
}
/**
* Reads, one time, values from the camera that are needed by the app.
*/
@SuppressWarnings("deprecation")
void initFromCameraParameters(Camera camera) {
Camera.Parameters parameters = camera.getParameters();
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
// We're landscape-only, and have apparently seen issues with display thinking it's portrait
// when waking from sleep. If it's not landscape, assume it's mistaken and reverse them:
/* Modified for portrait by join */
/*if (width < height) {
Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
int temp = width;
width = height;
height = temp;
}*/
screenResolution = new Point(width, height);
Log.i(TAG, "Screen resolution: " + screenResolution);
/* Modified for portrait by join */
if (CaptureActivity.orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
Point screenResolutionForCamera = new Point();
screenResolutionForCamera.x = screenResolution.x;
screenResolutionForCamera.y = screenResolution.y;
// preview size is always something like 480*320, other 320*480
if (screenResolution.x < screenResolution.y) {
screenResolutionForCamera.x = screenResolution.y;
screenResolutionForCamera.y = screenResolution.x;
}
cameraResolution = findBestPreviewSizeValue(parameters, screenResolutionForCamera);
} else {
cameraResolution = findBestPreviewSizeValue(parameters, screenResolution);
}
Log.i(TAG, "Camera resolution: " + cameraResolution);
}
public void setAspectRatio(double ratio) {
if (ratio <= 0.0) {
throw new IllegalArgumentException();
}
if (isInEditMode() || ((Activity)getContext()).getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
ratio = 1 / ratio;
}
if (mAspectRatio != ratio) {
mAspectRatio = ratio;
requestLayout();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
injectorPresenter();
super.onCreate(savedInstanceState);
Icepick.restoreInstanceState(this, savedInstanceState);
if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
setContentView(getLayoutResId());
initializeToolbar();
navigator = getAppComponent().navigator();
}
private void releaseVideoView() {
mVideoView.release();
if (mVideoView.isFullScreen()) {
mVideoView.stopFullScreen();
}
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
mCurPos = -1;
}
private void releaseVideoView() {
mVideoView.release();
if (mVideoView.isFullScreen()) {
mVideoView.stopFullScreen();
}
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
mPIPManager.setPlayingPosition(-1);
}
/**
* This method shall be called by the subclasses when they have valid
* object and want it to be delivered to external client (via callback) and
* then displayed on the screen.
* @param frame - the current frame to be delivered
*/
protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
Mat modified;
if (mListener != null) {
modified = mListener.onCameraFrame(frame);
} else {
modified = frame.rgba();
}
boolean bmpValid = true;
if (modified != null) {
try {
Utils.matToBitmap(modified, mCacheBitmap);
} catch(Exception e) {
Log.e(TAG, "Mat type: " + modified);
Log.e(TAG, "Bitmap type: " + mCacheBitmap.getWidth() + "*" + mCacheBitmap.getHeight());
Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
bmpValid = false;
}
}
if (bmpValid && mCacheBitmap != null) {
Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
int degrees = 0;
if (!isEmulator()) { // Rotation is always reported as portrait on the emulator for some reason
int orientation = getScreenOrientation();
switch (orientation) {
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
degrees = -90;
break;
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
break;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
degrees = 90;
break;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
degrees = 180;
break;
}
}
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
Bitmap outputBitmap = Bitmap.createBitmap(mCacheBitmap, 0, 0, mCacheBitmap.getWidth(), mCacheBitmap.getHeight(), matrix, true);
if (outputBitmap.getWidth() <= canvas.getWidth()) {
mScale = getRatio(outputBitmap.getWidth(), outputBitmap.getHeight(), canvas.getWidth(), canvas.getHeight());
} else {
mScale = getRatio(canvas.getWidth(), canvas.getHeight(), outputBitmap.getWidth(), outputBitmap.getHeight());
}
if (mScale != 0) {
canvas.scale(mScale, mScale, 0, 0);
}
if (BuildConfig.DEBUG)
Log.v(TAG, "mStretch value: " + mScale);
canvas.drawBitmap(outputBitmap, 0, 0, null);
if (mFpsMeter != null) {
mFpsMeter.measure();
mFpsMeter.draw(canvas, 20, 30);
}
getHolder().unlockCanvasAndPost(canvas);
}
}
}
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Mat mGrayTmp = inputFrame.gray();
Mat mRgbaTmp = inputFrame.rgba();
// Flip image to get mirror effect
int orientation = mOpenCvCameraView.getScreenOrientation();
if (mOpenCvCameraView.isEmulator()) // Treat emulators as a special case
Core.flip(mRgbaTmp, mRgbaTmp, 1); // Flip along y-axis
else {
switch (orientation) { // RGB image
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
if (mOpenCvCameraView.mCameraIndex == CameraBridgeViewBase.CAMERA_ID_FRONT)
Core.flip(mRgbaTmp, mRgbaTmp, 0); // Flip along x-axis
else
Core.flip(mRgbaTmp, mRgbaTmp, -1); // Flip along both axis
break;
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
if (mOpenCvCameraView.mCameraIndex == CameraBridgeViewBase.CAMERA_ID_FRONT)
Core.flip(mRgbaTmp, mRgbaTmp, 1); // Flip along y-axis
break;
}
switch (orientation) { // Grayscale image
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
Core.transpose(mGrayTmp, mGrayTmp); // Rotate image
if (mOpenCvCameraView.mCameraIndex == CameraBridgeViewBase.CAMERA_ID_FRONT)
Core.flip(mGrayTmp, mGrayTmp, -1); // Flip along both axis
else
Core.flip(mGrayTmp, mGrayTmp, 1); // Flip along y-axis
break;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
Core.transpose(mGrayTmp, mGrayTmp); // Rotate image
if (mOpenCvCameraView.mCameraIndex == CameraBridgeViewBase.CAMERA_ID_BACK)
Core.flip(mGrayTmp, mGrayTmp, 0); // Flip along x-axis
break;
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
if (mOpenCvCameraView.mCameraIndex == CameraBridgeViewBase.CAMERA_ID_FRONT)
Core.flip(mGrayTmp, mGrayTmp, 1); // Flip along y-axis
break;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
Core.flip(mGrayTmp, mGrayTmp, 0); // Flip along x-axis
if (mOpenCvCameraView.mCameraIndex == CameraBridgeViewBase.CAMERA_ID_BACK)
Core.flip(mGrayTmp, mGrayTmp, 1); // Flip along y-axis
break;
}
}
mGray = mGrayTmp;
mRgba = mRgbaTmp;
return mRgba;
}
public static ViewAction orientationPortrait() {
return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
private int getScreenOrientation() {
int rotation;
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD) {
rotation = ((Activity) context).getWindowManager().getDefaultDisplay().getRotation();
} else {
rotation = ((Activity) context).getWindowManager().getDefaultDisplay().getOrientation();
}
DisplayMetrics dm = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int orientation;
// if the device's natural orientation is portrait:
if ((rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90
|| rotation == Surface.ROTATION_270) && width > height) {
switch(rotation) {
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;
default:
Log.e(TAG, "Unknown screen orientation. Defaulting to " +
"portrait.");
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
}
}
// if the device's natural orientation is landscape or if the device
// is square:
else {
switch(rotation) {
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;
default:
Log.e(TAG, "Unknown screen orientation. Defaulting to " +
"landscape.");
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
}
}
Log.v(TAG, "Orientation: " + orientation);
return orientation;
}
protected void init() {
mOrientationEventListener = new OrientationEventListener(mActivity.getApplicationContext()) {
@SuppressLint("SourceLockedOrientationActivity")
@Override
public void onOrientationChanged(int rotation) {
boolean autoRotateOn = (Settings.System.getInt(mActivity.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1);
if (!autoRotateOn && mRotateWithSystem) {
if (!mIsOnlyRotateLand || getIsLand() == LAND_TYPE_NULL) {
return;
}
}
if (mVideoPlayer != null && mVideoPlayer.isVerticalFullByVideoSize()) {
return;
}
if (mIsPause) {
return;
}
// 设置竖屏
if (((rotation >= 0) && (rotation <= mOrientationOption.getNormalPortraitAngleStart()))
|| (rotation >= mOrientationOption.getNormalPortraitAngleEnd())) {
if (mClick) {
if (mIsLand > LAND_TYPE_NULL && !mClickLand) {
return;
} else {
mClickPort = true;
mClick = false;
mIsLand = LAND_TYPE_NULL;
}
} else {
if (mIsLand > LAND_TYPE_NULL) {
if (!mIsOnlyRotateLand) {
mScreenType = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (mVideoPlayer.getFullscreenButton() != null) {
if (mVideoPlayer.isIfCurrentIsFullscreen()) {
mVideoPlayer.getFullscreenButton().setImageResource(mVideoPlayer.getShrinkImageRes());
} else {
mVideoPlayer.getFullscreenButton().setImageResource(mVideoPlayer.getEnlargeImageRes());
}
}
mIsLand = LAND_TYPE_NULL;
}
mClick = false;
}
}
}
// 设置横屏
else if (((rotation >= mOrientationOption.getNormalLandAngleStart())
&& (rotation <= mOrientationOption.getNormalLandAngleEnd()))) {
if (mClick) {
if (!(mIsLand == LAND_TYPE_NORMAL) && !mClickPort) {
return;
} else {
mClickLand = true;
mClick = false;
mIsLand = LAND_TYPE_NORMAL;
}
} else {
if (!(mIsLand == LAND_TYPE_NORMAL)) {
mScreenType = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if (mVideoPlayer.getFullscreenButton() != null) {
mVideoPlayer.getFullscreenButton().setImageResource(mVideoPlayer.getShrinkImageRes());
}
mIsLand = 1;
mClick = false;
}
}
}
// 设置反向横屏
else if (rotation > mOrientationOption.getReverseLandAngleStart()
&& rotation < mOrientationOption.getReverseLandAngleEnd()) {
if (mClick) {
if (!(mIsLand == LAND_TYPE_REVERSE) && !mClickPort) {
return;
} else {
mClickLand = true;
mClick = false;
mIsLand = LAND_TYPE_REVERSE;
}
} else if (!(mIsLand == LAND_TYPE_REVERSE)) {
mScreenType = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
if (mVideoPlayer.getFullscreenButton() != null) {
mVideoPlayer.getFullscreenButton().setImageResource(mVideoPlayer.getShrinkImageRes());
}
mIsLand = LAND_TYPE_REVERSE;
mClick = false;
}
}
}
};
mOrientationEventListener.enable();
}