libgdx custom ViewPort - android

I need a ViewPort with following the properties :
maintain aspect ratio
fill x axis
always align the top of the screen
cut the bottom if screenHeight < viewPortHeight
Do I need to extend ViewPort or use existing implementations with some configurations, to achieve such behavior ?

Related

AppCompatImageView displaying big image using ScaleType.CENTER and the use of padding

I have an image 3264x1836 and I display it in a custom AppCompatImageView having dimensions 1280x720. I want to preserve the image size so I use ScaleType.CENTER, but I also want that its top-left corner is positioned at the coordinates 0,0 of my custom view, so I set the padding left and top accordingly.
Now, to achieve that I had to use 3264-1280 (the difference between the widths) as left padding and 1836-720 (the difference between the heights) as top padding while, in my opinion, these values should be both divided by 2.
Can somebody explain why?
It is likely that you are breaking the AppCompatImageView code by asking it to do something that wasn't anticipated or tested.
While the above could still be true, the padding that you are applying is correct. Here are the calculations:
For simplicity, let's take a look at the left padding needed to shift the graphic. The same sort of calculations will work for the top padding.
The width of the graphic is greater than the width of the ImageView.
d = wgraphic - wimageview
If the graphic is centered in the ImageView, then the overhang on the left and right sides will be 1/2 the difference in widths.
s = d / 2
The graphic is centered within the padding of the ImageView. The amount of padding that has to be specified must be just enough to shift the center of the graphic by the amount s.
The initial center of the ImageView without padding is
ci = wimageview / 2
The shifted center with left padding ( p ) is
cs = p + (wimageview - p) / 2
So,
s = cs - ci = p + (wimageview - p) / 2 - (wimageview / 2)
Solving for the padding needed for a shift of s we get p = 2s = d. In other words, the padding we need is twice the shift required which is what you are seeing.
A fix that doesn't involve padding would be to specify
android:scaleType="matrix"
You should remove the padding. The new scale type will apply the identity matrix to the image and place it in the top/left corner without resizing.

How do you set a percent width (or flex) for a React Native Picker on Android?

On iOS, you're able to put a <Picker> in a flex box and make the picker(s) flex the entire width of the screen.
Using the new cross-platform <Picker> element in React Native, what's the easiest way to do this on Android?
The Picker on Android only accepts the width element, and obviously doesn't accept percentage values. It does not scale to the width of its container.
You can set a percentage by getting the width of the screen using Dimensions:
import {
Dimensions
} from 'react-native'
var width = Dimensions.get('window').width;
var fifyPercent = width / 2
var oneHundredPercent = width
Or if you are measuring a component and not the entire device width you can get it's width by using the onLayout function, then use it instead.
onLayout
onLayout implementation
You can kind of set a percentage using layout_weight.
This explanation is extracted from this question.
With layout_weight you can specify a size ratio between multiple
views. E.g. you have a MapView and a table which should show some
additional information to the map. The map should use 3/4 of the
screen and table should use 1/4 of the screen. Then you will set the
layout_weight of the map to 3 and the layout_weight of the table to 1.
To get it work you also have to set the height or width (depending on
your orientation) to 0px.
So, your iOS flex box will be a LinearLayout.
These are also very good and ilustrated examples:
http://www.101apps.co.za/index.php/articles/using-android-s-layout-weight-attribute.html
http://javarticles.com/2015/04/layout_weight-android-example.html

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.

Size and position of GLSurfaceView

Actually I have project in which I'm using GLSurfaceView. At this moment this component is placed as main control on whole screen. In this configuration I'm able proper handling drawing functionality.
In next step I want to change size and position of GLSurfaceView. I want to place it in the center of the screen and set width and hight to exact phicical dimmension for example 20mm x 20mm.
Do you have any advices or hints how should I start to introduce this kind of changes to GLSurfaceView?
I want to place it in the center of the screen
You need to set gravity to the parent element like Gravity.CENTER. For example if it's LinearLayout then call linearLayout.setGravity(Gravity.CENTER).
Set width and hight to exact phicical dimmension for example 20mm x
20mm
To calculate size from mm to pixels use:
float mmInPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 20,
getResources().getDisplayMetrics());
And to set size for GLSurfaceView use LayoutParams:
ViewGroup.LayoutParams layoutParams=glSurfaceView.getLayoutParams();
layoutParams.width=mmInPx;
layoutParams.height=mmInPx;
glSurfaceView.setLayoutParams(layoutParams);

Categories

Resources