Comparing MotionEvent.getX() with screen width in Android - android

I've created an OnTouchListener for motion events in an activity, and I want to find where they happen relative to the screen.
I can use event.getRawX() to find the x co-ordinate, and similarly windowManager.getDefaultDisplay().getMetrics(new DisplayMetrics()) or ...getSize(new Point()) to get the display dimensions.
The problem I'm having is that the display dimensions are consistently reporting the width as 1080, but the motionevents I receive return numbers greater than 1800 for getRawX().
Using the deprecated display.getWidth() returns something close, around 1700 - but even this is lower than what's reported at the right edge of the screen.
I assume motion events use a different co-ordinate system to the display metrics - but how can I convert between the two?
Cheers
Nic

So, I have an answer. This is down to my own stupidity.
What I forgot to mention is that I am viewing the screen in landscape mode. Using v.getWidth(), rather than the display dimension, returns 1920, which makes much more sense.
So obviously (with hindsight) the view width takes into account the orientation, which the display width doesn't - so I've been reading the portrait width (1080) rather than the landscape width/portrait height (1920).
Goodness knows what display.getWidth() was returning...
Thanks to anyone who read and thought about this, and Mihriban for reformatting it.

Related

how to get fullscreeen height on S9 and similar?

My current android code to retrieve the screen height in pixels is this:
Resources.getSystem().getDisplayMetrics().heightPixels
However when the app is running in full screen mode on Samsung S9 (and probably on similar devices) the returned height is the same as it is when the app is not running fullscreen, i.e. it is calculated without the buttons menu bar that is gone when we're in fullscreen.
Any ideas on a better way to retrieve the height or possibly to be aware of the fact that the app is running full screen?
You can use
val size = Point()
windowManager.defaultDisplay.getRealSize(size)
size.y // will be your needed height
or in Java
Point size = new Point()
getWindowManager().getDefaultDisplay().getRealSize(size)
size.y // this one will be your height
Hope it's what you need.

Restricting an app's screen size based on a phone's resolution

I am developing an application in android studio which is displayed correctly on 1080x1920 phones(and lower) but for phones like mine(s8) the app gets messed up and i can't find a way to fix the app's ui for every screen size. So, i was wondering if there is a way to restrict the app's screen size to 1080x1920(with let's say black bars at the top & bottom of the screen) even if a phone's dimensions are bigger than that(this is automatically done in games that are not compatible with s8). Do you guys know a way of achieving this effect?
It's possible, you can set your parent View to exact size given the aspect ratio you want (1080x1920 scales down to 9x16). For this you'd have to programmatically call the View (or create it in code) and set width and height in pixels with LayoutParams:
yourView.setLayoutParams(new LayoutParams(width, height));
for the weight and height you'd want the actual screen dimensions, which you can get from the WindowManager:
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int x = point.x;
int y = point.y;
Here you get the x and y, which hold your actual screen size (in pixels). Then, you decide on which one should be reduced (in order to maintain 9x16 aspect ratio) and set your parent View's LayoutParams as mentioned above.
Finally, your set your content View as your parent View:
setContentView(yourView);
That's about it. Hope that helps.
EDIT: Since you've mentioned games, I can tell you that there (if your use Android SDK) you have your SurfaceView as a parent View, the procedure is same.

how can I fit toggles/buttons into scrolled panel on different screens

I am trying to make a game in Unity3d 5. What I am trying to do right now is to make a scrollable panel filled with toggles/buttons. The panel should fit the frame on the screen and is masked so it hides under image frame. The concept is under this link:
https://www.dropbox.com/s/9zxwx33orz46q60/Screenshot2.png?dl=0
As you can see I managed to make toggles fill the panel, but not exactly as it should be. I us content size fitter on panel with both vertical and horizontal fitting set as preferred size. Also I put LayoutElement on panel and set min width and min height as 200. There is VerticalLAyoutGroup with only "Height" checked.
The panel is filled during runtime with quite simple prefab. It has preferred width also set at 200.
So it would not be much problem but when I buid it and run on Galaxy S5 it looks like this:
https://www.dropbox.com/s/9zrv6kyf4n0ueam/Screenshot1.png?dl=0
So question is: how can I make it to always fit the panel? I was changing a lot of different setting and what I once managed to do was that toggles/prefabs did fit the width of the panel but did not keep aspect ratio about 3 (they were flat and unreadable. On other instances the panels overlap partially or completely.
Please help.
EDIT
I managed to achieve this:
https://www.dropbox.com/s/6w80qnuekqyrx96/Screenshot3.png?dl=0
With settings as:
Panel:
* Content Fitter: horizontal - unconstrained, veertical - preferred size
* Child Force expand - both height and width checked
* Anchors and size stretched to fit scroll rect
Once again I struggle with something for few days, finally post a question here... and solve a problem in few minutes.
So the problem was in keeping aspect ration while resizing the prefab. When it was resized horizontally to fit panel it might be resized vertically too, but the position was not adjusted to compensate this change. So what was needed was to change the preferred height of the prefab on the basis of panel width (could not use toggle width, since it was not right yet (not resized yet)). In the code where I create togges for every toggle I had to put (3f is my aspect ratio):
float width = contentPanel.GetComponent<RectTransform> ().rect.width;
newPower.GetComponent<LayoutElement> ().preferredHeight = width / 3f;
Other settings are:
Panel uses vertical fit as preffered and horizontal as unconstricted
Toggle uses aspect ratio fitter set as Width controls height and aspect as 3

Setting actor size according to screen size in libgdx

I am developing a simple game using libgdx in android. I created an actor. I want to resize the actor according to the screen size. Can anyone please suggest me a simple tutorial to do the same
well it depends on how you set the viewport of your Stage.
lets say your stage viewport is set to stage.setViewport(16,10,false) within your resize()-method.
This way, your screen will always show the region from (0,0) in the bottom left to (16,10) in the top-right corner...
If you create an Actor with width=8 and height=5, it will always be half of the screen width and half of the screen height big.
Of course, the example above will usually distort your screen when it resizes... So you could use the setViewport(16,10,true); This way, you will keep the aspect ratio, but might need to resize or reposition some actors within the resize() method, e.g. when they are to be shown on the right edge of the screen.
The other way is by setting the viewport to the same coordinate systems as the pixels on the screen: stage.setViewport(width,height,false); (within the resize(...)-method). This way, one unit in the stage coordinate system equals one pixel on the screen.
When doing so, you will have to resize the actors in the resize() method after setting the viewport:
actor.setSize(0.2f*stage.getWidth(), 0.2f*stage.getWidth() * actor.getHeight()/actor.getWidth()):
Doing so will always make the actor be exactly one fifth of the screen width and have its height matched so it doesn't get distorted.
Hope this helps a bit...
If you are using the latest version (nightlies) you may have seen the Viewport class.
This class is what you are looking for. Lets say you have a 1600 * 900px monitor and you set your Viewport to 16 * 9, every unit in your Viewport will be 100px on your Screen.
This means, if you set your Stages Viewport to 16 * 9 an Actor with the size of 1 will be 100*100px big. If you are using a 800*450px monitor it will be 50*50px big.
There are also different Viewport types:
StretchViewport: Your Virtual Viewport gets stretched to fill the whole Screen. So if you have a 1600 * 900 monitor and a Virtual Viewport of 16/10, a 1x1 Actor will be 100px wide, but only 90px heigh. So the aspect ratio is not the same anymore.
FitViewport: If your Virtual Viewport has a different aspect ratio the Screen will be filled with black bars. So on a 1600*900 Screen with 16/10 virtual Viewport a collumn of 80px width on both sides of the Screen will be black ((virtualWidth / wirtualHeight) * realHeight = realWidth, physicalWidth - realWidth = blackWidth, to have the game in center blackWidth/2 on both sides), to keep aspect ratio.
There are also others, but those are the most important 2 imho. You can read more on the article i linked.
If you are not using the latest nightlies, you need to use camera. The constructor of the OrthographicCamera takes the viewport width and height. Those are the same as for the viewport. The difference is, that it is a "StretchViewport" automatically, so you have to manually set the glViewport like in this tutorial.
I hope i could help.

Setting width / height after using setRotation

I'm building an Android app which will be used on Mini PC's / Cyclone devices powered by Android and one requirement I have is to force the app to switch into portrait orientation. My first thought was to set the orientation programmatically like so:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Which works on tablets / phones but not on Mini / Cyclone PC's - the orientation remains landscape. My next thought was to use the setRotation() method of the RelativeLayout (which is my root container) to rotate it 90 degrees - that works great but the width / height doesn't adapt itself to the size of the screen. What I end up with is the RelativeLayout rotated by 90 degrees but the width and height are still in landscape. I tried manually setting the width / height of the RelativeLayout control by setting it's LayoutParams and although that sorted out the width, the height is incorrect as it only takes up half the screen. This is what I'm currently doing:
m_container.setRotation(-90);
RelativeLayout.LayoutParams portraitParams = new RelativeLayout.LayoutParams(Util.getDisplayHeight(MainActivity.this), Util.getDisplayWidth(MainActivity.this));
m_container.setLayoutParams(portraitParams);
The getDisplayHeight/Width methods correctly returns the width / height of the screen.
Can anyone point out what I'm doing wrong / have any suggestions how to get the width / height set correctly of my RelativeLayout m_container?
Many thanks,
Tony
Did you try setting android:screenOrientation="portrait" in the AndroidManifest.xml on the relevant activities ?

Categories

Resources