Android: Camera preview is tilted - android

I have made a program implementing Camera on surface view.
In landscape mode, the preview is correct, but in portrait mode the preview is tilted 180 degrees, it is not coming upright.

If you are okay with your app always being in landscape mode, then you should add this line to your manifest and it will get rid of your problem:
<activity android:name=".YourActivity"
android:label="#string/app_name"
android:screenOrientation="landscape">
However, if you do want to want a portrait mode application then, you can add this to your manifest:
<activity android:name=".YourActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
as long as you add the mCamera.setDisplayOrientation(90); line to your activity (provided you check for android version greater than or equal to Froyo.

Related

LibGDX Game set to landscape, but showing up as portrait only on certain devices

I’m making a LibGDX (1.9.9) game that is meant to be fixed in landscape mode, but when I run it on certain devices, it shows up in portrait mode.
I’ve tested it on several physical and virtual devices ranging from Android 4 to 11 as well as on an iPhone simulator through RoboVM (2.3.9). This happens on an android virtual tablet (Android 11) and the iPhone simulator. It has gotten stuck in portrait a couple times on a physical device as well, but I have not been able to reproduce it. I haven’t been able to understand why this could be.
I have landscape (android:screenOrientation="landscape”) set in the android manifest.
Gdx.input.getRotation() returns 90 when it is properly set on landscape, but 0, when in portrait so perhaps I can try checking if it is 0 and change the rotation dynamically, but I have not been able to find out how to do that either.
I’ve tried a workaround where I check if screen width < screen height and rotate the camera 90 degrees if that is true to put it into landscape manually, but that causes the camera to become distorted in the manner shown below even though I know I’ve modified the aspect ratio correctly for the rotation.
How camera distorts when trying to rotate to landscape
I am using OrthographicCamera without any Viewports.
// Screen dimensions
width = ((float) Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight()) * GameSpecs.WORLD_HEIGHT;
height = GameSpecs.WORLD_HEIGHT;
System.out.println("width: " + width);
System.out.println("height: " + height);
System.out.println("orientation: " + Gdx.input.getNativeOrientation());
System.out.println("rotation: " + Gdx.input.getRotation());
// If showing portrait and needs to be rotated
rotateForProperLayout = width < height;
if (rotateForProperLayout) {
height = ((float) Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth()) * GameSpecs.WORLD_HEIGHT;
width = GameSpecs.WORLD_HEIGHT;
}
cam = new OrthographicCamera(width, height);
// Rotate camera to be in landscape
if (rotateForProperLayout) {
cam.rotate(-90);
}
cam.translate(cam.viewportWidth / 2f, cam.viewportHeight / 2f);
cam.update();
My Android Manifest...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mydev.mygame” >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:isGame="true"
android:appCategory="game"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<activity
android:name="com.mydev.mygame.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation=“landscape"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Any help in trying to understand the reason for this or possible workarounds would be greatly appreciated. Please let me know if I should clarify anything or provide more information.
Thanks in advance.
Since no ones helped you so far I'll post my android manifest, you should maybe post yours since this is almost certainly where the problem is. I set this up a long time ago and, to be honest, can't really remember exactly why everything is set as it is, but it works for me. Note: I use device rotation to control my game in some modes.
As for your manual screen rotation issues, you could access the cameras projection matrix and scale it accordingly to get it looking ok. I do not recommend this though, since it is difficult to track how screen resize calls should alter your layout when you also have to track screen rotation. You would have to consider if the width change should actually be considered a height change when in portrait mode and vice versa (depending on your game complexity). I would recommend just sorting out your manifest. Good luck.
android:screenOrientation="fullSensor"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<activity
android:name="com.funferret.tr.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="sensorLandscape"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

How to remember last screen orientation setting in android

I have a android 6.0.1 tablet. My activity is here using following orientation settings:
<activity
android:name=".MainActivity"
android:screenOrientation="sensorLandscape"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize|stateHidden"
android:configChanges="keyboardHidden|orientation|screenSize|uiMode">
When i turn tablet into 180 degree application activity rotate but problem is that when i turn on and off screen using power button on tablet while tablet is in 180 degree, application took few second to rotate. This was not happing in older android version. Is there any android activity setting which i can use to keep remember last screen orientation and keep it same either tablet screen turn off and on ?
Simply use this in onCreate of your activity to disable rotation of activity when screen turn on and off:
((KeyguardManager)getSystemService("keyguard")).newKeyguardLock("keyguard").disableKeyguard();

I want my android application in landscape and portrait mode only?

I am developing one camera application.I want my application should be in landscape and portrait only.If mobile changes to reverse landscape, application should remain in portrait or landscape mode.And one thing is if device rotated to landscape application should change to landscape, and device changes to portrait, application changes to portrait.
If you want Potrait Mode then:
<activity android:name=".ActivityName" android:screenOrientation="portrait" />
If you want Landscape Mode then:
<activity android:name=".ActivityName" android:screenOrientation="landscape" />
You can use android:screenOrientation= locked on android manifest.
See: http://developer.android.com/guide/topics/manifest/activity-element.html for more info

Tablet devices loading wrong orientation

My app only uses one page which is the main activity and in the manifest it is set not to allow rotation hence the orientation stays the same wether or not you rotate the device. however on tablets when a user is in landscape orientation and runs the app. the app loads the default orientation (Portrait) but in a landscape view instead and im seeing only half the screen.
help.
This is part of my activity in manifest i use nosensor to disable rotation.
<activity
android:name="com.paul.xicon.MainActivity"
android:windowSoftInputMode="adjustPan"
android:label="#string/app_name"
android:screenOrientation="nosensor">
I'm not sure how are you disabling the screen rotation. But this should work just fine in your activity tag of manifest.
android:screenOrientation="landscape" (disables the screen-rotation & restricts to landscape mode)
or,
android:screenOrientation="portrait" (disables the screen-rotation & restricts to portrait mode)

Android Application portrait mode

I am developing an android application. This Application is in Portrait mode. I changed the manifest file like below
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/customTheme"
android:allowBackup="true">
<activity android:name=".Favorites"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DBHelper">
</activity>
<activity android:name=".Find"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
</application>
which is working fine upto large screen. When I opened it in Extra-large screen the screen is not coming good, i.e all the controls in the view are diagonally cutting.
see the image:
When I remove the android:screenOrientation="portrait" tag from the manifest file my Xlarge Screen is appearing normally. I am not able to understand the problem exactly can any one help me in solving this. I want my application should be in portrait mode only.
You need to provide layout for xlarge-port..So that ur layout can adjust in xlarge screen.
You can keep different layouts for different screen sizes,For that you need to create different layout for different screen size and put it into different folders named as
layout-large : for portrait mode of large size screen
layout-medium :for portrait mode of medium size screen
layout-xlarge :for portrait mode of xlarge size screen
layout-large-land :for landscape mode of large size screen
layout-medium-land :for landscape mode of medium size screen
layout-xlarge-land :for landscape mode of xlarge size screen
Now Android OS will choose appropriate screen automatically.

Categories

Resources