My android application uses the Y Axis accelerometer to control the character in the game.
This is working fine on most mobile phones, however on my Galaxy Tab 2 10.1, the axis seems to be reversed and I have to manualy change the input for it work.
I would like to write a simple conditional statement that would swap based on device type, however I am unsure what the best practice for this would be. As I am sure someone would have come accross this issue before I ask the question.
How can I determine if the device is a table or a mobile in order to change Accelerometer axis?
I think this relates to the default orientation of the device, which tends to be portrait for phones, or landscape for tablets. There's a question here about exactly that: How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout) , and based on my experiences I'd say that this is the best answer . An important point to understand is that whatever the display is doing (landscape, portrait, etc), the x-y-z axes used by the sensors don't change.
I found out the best way to do this is to check the screen size on the device and base a boolean conditional to device on which axis to use. This is tested and working great.
// Check the screen layout to determine if the device is a tablet.
public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
// Changle Accelx or Accely input based on the above (Im using
OpenGLES 1.1)
if (isTablet(glGame)) {
world.update(deltaTime, game.getInput().getAccelX());
}
if (!isTablet(glGame)) {
world.update(deltaTime, game.getInput().getAccelY());
}
Related
I'm a newbie in mobile development and I try to develop an Android application for several models of smartphones and tablets.
Now I'm experiencing some problems with selecting an appropriate layout parameters for Xiaomi Redmi 6a [https://www.gadgetsnow.com/mobile-phones/Xiaomi-Redmi-6A]
I designed a layout specially for it : land-xhdpi-1440x720
But when I try to run my app it seems to be, that it selects land-xhdpi-800x480.
Why so? What Am I doing wrong in this case?
And, by the way, could you recommend me some articles about layout selection for different types of devices based on some real experience (not an Android Developers Manual)?
It's considered bad practice. Why do you need layout for particular device?
Screen dimension for resource qualifier is set in dp, not in pixels. For xhdpi screen with 1440x720 px, screen size will be 720x360 dp
According to the layout folder spec it does not appear to allow specifying both screen dimensions in a layout folder name.
If you want to check the device model in your app you can use the Build class (the BRAND, MODEL and DEVICE fields) and select the appropriate layout at runtime by calling setContentView(R.layout.*) in your Activity/Fragment.
However, you should be aware that matching the device model to the screen resolution can be challenging, especially since on some phones the resolution can be changed in settings.
It would be safer to check the actual resolution at runtime:
DisplayManager mgr = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE)
Display[] allDisplays = mgr.getDisplays();
Display targetDisplay;
if (allDisplays != null && allDisplays.length > 0) {
//select the display to use, usually there is only one (or find the one with the highest resolution)
targetDisplay = allDisplays[0];
//get the resolution
Point sizePt = new Point();
targetDisplay.getRealSize(sizePt);
//or
DisplayMetrics metrics = new DisplayMetrics();
targetDisplay.getRealMetrics(metrics);
}
Also note the actual values may not be equal to the physical resolution due to the height of the notification and navigation bars so be sure to run some tests and try to use more general comparisons than
sizePt.x == 1440 && sizePt.y == 720
Is there a way to set the LED intensity that I desire? I know that to turn on the LED I use:
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mycam.setParameters(p);
But this code just turns on the LED. But is there a way to set different intensities to the LED for a stronger light or to reduce the light intensity?
HTC has an alternative API that supports this, however it's only on HTC Sense devices, and as of Gingerbread they've changed the permissions so it's only for their Flashlight app, not third party ones (unless you use root).
But on 2.2 HTC devices you can use it by writing a string to /sys/devices/platform/flashlight.0/leds/flashlight/brightness. This controls if the LED is on and how bright it is. For maximum brightness write "128\n", half brightness write "64\n". Easy to test from adb shell:
echo "128" > /sys/devices/platform/flashlight.0/leds/flashlight/brightness
And finally turn it off by writing "0\n" there.
Short answer is: NO.
Longer answer - maybe on some devices using undocumenterd calls / parameters.
Supported flash modes, and their meanings (and behaviours) differ from device to device.
Your best option is to query supported flash modes, and hope they work as intented.
Try to set different Flash Modes available for Camera parameters.
* FLASH_MODE_OFF
* FLASH_MODE_AUTO
* FLASH_MODE_ON
* FLASH_MODE_RED_EYE
* FLASH_MODE_TORCH
you can set Flash mode using setFlashMode() method.
This was just for camera back light. but if you want to change complete screen intensity. have a look at the example here.
Try to find the code of this function:
private native final void native_setParameters(String params);
I beleive that you will find out if this is possible when you look through it.
Looking at the publics, it seems impossible
I have tried this in my Samsung Galaxy S3 Mini. I have not tried on other devices.
Whenever i do this (while the led is ON):
public void changeIntensity()
{
cam.stopPreview();
cam.startPreview();
}
The LED rotates between 3 levels of intensity. It makes no sense, but it works.
I have been set a project for college that needs to make use of the Android Gyroscope Functionality, the problem is, my application is quite simple in that it just displays information about a sports event to the user (e.g. current games, stadium info, team stats etc).
I initially thought that I could make use of the Gyroscope to change my layout to a different style depending on the orientation of the screen (portrait and landscape) but from what I know now, it seems that this isn't really something that the Gyroscope is used for? correct me if i'm wrong?
My question is, what could I use the gyroscope for for a simple like I mentioned?
Android already handles portrait and landscape orientation so as you mentioned there is no need to use gyroscope for this. I believe its the most popular usage is for a game controller when you should rotate your device to do something.
But sure you can use it for something similar in your app. Like to display an image always horizontally despite of the device rotation.
I am creating an Android game that uses the accelerometer for X-axis and Y-axis movement in landscape orientation. The X-axis works as expected, however the Y-axis is causing an issue.
For the Y-axis to work properly, the Y-axis needs to be at 0 (The phone is perfectly screen-up.) This is an issue because I don't expect users to be hunched over their phones to play properly.
I attempted to correct this by taking the initial reading of the Y-axis orientation and subtracting that from the following Y-axis readings, but if the user begins the game at -10 (Phone screen directly facing them) the phone will not register any further tilting back.
Does anyone know a better way to handle this situation? Thank you all for the help thus far!
It's possible. Check out Allowing both landscape options and accelerometer with libgdx
and a fix for accelerometer reading
I need to determine the actual orientation of the phone which is running my app and I can't find any way to do it. I have found the "getRequestedOrientation" function but it only retrieves the orientation set by the "setRequestedOrientation" function.
In my app, I let the phone choose the orientation according to it's physical orientation. And I have some customizing to do depending on the result.
How can I do?
Thanks in advance for the time you will spend trying to help me.
According to these two questions:
Check orientation on Android phone
How can I get the current screen orientation?
you can use:
Activity.getResources().getConfiguration().orientation
According to the comment on the second question, however, this value returns only landscape
or portrait. If you need reverse orientation as well, I would suggest querying the accelerometer
values to detect the difference. However, I'm not familiar with Android and
accelerometers enough to suggest how to do so exactly.
Use getRotation()
Its really to get the current orientation.
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();
See this, this, and this stackoverflow question. They all address how to get the Android phone orientation and the second one includes a detailed explanation of how the Android compass and orientation system works.