Scaling issue with mobile flex app - apache-flex

I have a couple of AS3 games that I want to run in a flex mobile app. I put my original games into a single library and then added it to my mobile app. So far so good.
The problem I get is when the game starts it doesn't scale itself to the StageScaleMode.SHOW_ALL I have specified in the games.
I'm starting the games like this:
var game:MyGame = new MyGame();
var container:UIComponent = new UIComponent();
addElement(container);
container.addChild(game);
this.actionBarVisible = false;
I tried setting the same scale option to the stage in my mxml but it doesn't change anything.
Any ideas?
Thanks.

Mobile device screens have varying screen densities, or DPI (dots per inch). You can specify the DPI value as 160, 240, or 320, depending on the screen density of the target device. When you enable automatic scaling, Flex optimizes the way it displays the application for the screen density of each device.
For example, suppose that you specify the target DPI value as 160 and enable automatic scaling. When you run the application on a device with a DPI value of 320, Flex automatically scales the application by a factor of 2. That is, Flex magnifies everything by 200%.
To specify the target DPI value, set it as the applicationDPI property of the tag or tag in the main application file:
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
firstView="views.HomeView"
applicationDPI="160">
If you choose to not auto-scale your application, you must handle the density changes for your layout manually, as required.
Devices can have different screen sizes or resolutions and different DPI values, or densities.
Resolution is the number of pixels high by the number of pixels wide: that is, the total number of pixels that a device supports.
DPI is the number of dots per square inch: that is, the density of pixels on a device’s screen. The term DPI is used interchangeably with PPI (pixels per inch).
applicationDPI (if setted) specifies the target DPI of the application. Flex automatically applies a scale factor to fit good on another devices with different DPI value.
Capabilities.screenDPI is the specific DPI value of the current device.
runtimeDPI is similar to Capabilities.screenDPI. This value is the current device DPI rounded to one of the constants defined by the DPIClassification class (160, 240 and 320 DPI).
If you want to know the real dimensions (width and height) of a component on the current screens you need to work with the scale factor as:
var scaleFactor:Number = runtimeDPI / applicationDPI;
var currentComponentSize:int =componentSize.height * scaleFactor;
If you haven’t access to applicationDPI and runtimeDPI values, you can calculate the scaleFactor manually using Capabilities.screenDPI as:
// Copy the applicationDPI setted in your application. ie:
var _applicationDPI:int = 160;
var _runtimeDPI:int;
if(Capabilities.screenDPI < 200)
_runtimeDPI = 160;
else if(Capabilities.screenDPI >=200 && Capabilities.screenDPI < 280)
_runtimeDPI = 240
else if (Capabilities.screenDPI >=280)
_runtimeDPI = 320;
var scaleFactor:Number = _runtimeDPI / _applicationDPI;
var currentComponentSize:int =componentSize.height * scaleFactor;
http://www.francescoflorio.info/?p=234

Related

how to transform a pixel to xamarin.forms units?

there are 160 units per inch.
2.If I created an Photoshop file that are 72 dpi then there will be 72 points per inch.
3.If the element is 88px height in Photoshop then what I have to set it in xamarin?
If the phone is 360dpi then the height in xamarin should be :88 / 72 * 160 / 2?
but it is not right.
I know the units in each platfform and I only want to know how to get units from pixels
You could use Xamarin.Essentials to get the Screen density as follows:
// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
// Screen density
var density = mainDisplayInfo.Density;
If the density is 3 in iOS device and pixels is 88 , then there are 88/3 units in iOS.
If the phone is 360dpi
That means it shoule be a Android device, and screen density also can be calculated by 360/160 = 2.25. Then there are 88/2.25 units to set for HeightRequest.
================================Update==============================
If there is a BoxView in Xaml as follows:
<BoxView x:Name="MyBoxView" BackgroundColor="CadetBlue" HorizontalOptions="Fill"/>
And the effect:
Now I will print the MyBoxView.Width. the result is:
Console.WriteLine("++++MyBoxView++++" + MyBoxView.Width);
++++MyBoxView++++411.428571428571
If you use var density = mainDisplayInfo.Density; to get density, you will get Screen Density::2.625. (My device is Piexl_2_pie_9_0 api 28 emulator)
You know the size of screen width is 1080 pixels, however the width is 411.428571428571. That should means units of WidthRequest.
And if you put 411.428571428571 * 2.625 , you will get 1080 pixels.

Can QFontMetrics account for windows dpi scaling?

I was under the impression that if you do this in your application
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication* app = new QApplication(temp, NULL);
then Fonts gets automatically scaled up on high resolution display. Same if you explicitly increase font scaling in Windows 10 (Settings-->System->Custom Scaling).
However, when running the following code with 100% and then 200% scaling in Windows 10, it does not return doubled size.
QFont font = QFont("arial", 10);
QFontMetrics fm(font);
int width = fm.width("abcdefgABCDEFG");
Strangely there is only 1 pixel difference.
100% --> width = 108 pixels
200% --> width = 109 pixels
Why is that? Can I get QFontMetrics to account for Windows scaling? Or do I need to use Logical / Physical DPI to deduce that font size must be increased by a factor 2?
Thanks
For the proper scaling of custom-drawn items use QScreen::physicalDotPerInch property to realize the scaling coefficient to apply to actual drawings:
qreal myScale = pScreen->physicalDotPerInch() / constStandardPerInch;
P.S. The question still needs to be revised.

Sprite initial scale based on size on screen

I have a Sprite object with a Width and a Height (texture size).
I want to display the sprite on the screen with the same size as the original texture size.
Because the scene size, the camera position, and texture sizes are not constant values I need some way to scale the Sprite.
Most of the time camera is Perspective but some times it can be Orthographic.
So I need 2 formulas for the scale.
I've found some answers on how to make the Sprite size constant when zooming but in this calculation the initial scale is unknown.
Thanks.
One way that I do something similar to this is by creating an initial(optimal) size of the screen, and scaling the node based on changes to the screen.
The easiest way to do this is to have an initial size of the scene in which your sprite is the exact size you want it. You would call this before doing any zooming and you only call it ONCE:
var initialScreenWidth = self.size.width
var initialScreenHeight = self.size.height
Now whenever you change the size of screen(such as zooming), you have to find the scale at which the screen has changed:
var scaleWidth = self.size.width/initialScreenWidth
var scaleHeight = self.size.height/initialScreenHeight
Now that you have those two scales, all you have to do is:
texture.xScale = scaleWidth
texture.yScale = scaleHeight
You will have to create the scales and set the texture dimensions every single time you change the size of the screen. (Most likely in the Update function so it looks smooth).
Hope this helps!

Display two object same real distance (e.g. inches) apart across different browers / screen sizes

I'm developing a psychology experiment in the browser. In order to keep the same viewing angle across people, I want to display two characters around 5 inches apart on the screen.
Is there any way to detect the real size of the monitor being used, and using the screen resolution and DPI, render the two objects the same real width apart? (I will only allow people that have real computers, e.g. not mobile)
I heard detecting real size may not be possible, if true, and assuming people will report to me the real size of their monitor, is this possible?
I'm using HTML5 Canvas, fwiw. Perhaps resizing this canvas w.r.t to the resolution and DPI is a solution.
No, unfortunately. The browser will always report 96 DPI. Without actual DPI you cannot produce exact measures in other units than pixels.
Even if you could the browser would only reflect the system DPI which in itself is just an approximation.
You need to "calibrate" for the individual device providing a mechanism to do so, e.g. a scale that can be varied and measure on screen. When it measures 1 inch you know how many pixels covers that inch, and then this value can be used as a scale for the rest.
Example on how to get screen DPI via "calibration"
var ctx = document.querySelector("canvas").getContext("2d"),
rng = document.querySelector("input");
ctx.translate(0.5, 0.5);
ctx.font = "16px sans-serif";
ctx.fillStyle = "#c00";
render(+rng.value);
rng.onchange = rng.oninput = function() {render(+this.value)}; // update on change
function render(v) {
ctx.clearRect(-0.5, -0.5, 600, 300);
ctx.strokeRect(0, 0, v, v);
ctx.fillText(v + " PPI", 10, 20);
// draw marks which should be 4 inches apart
ctx.fillRect(0, 0, 3, 150);
ctx.fillRect(96*4 * (v / 96), 0, 3, 150); // assuming 96 DPI base resolution
ctx.fillText("------ Should be 4 inches apart ------", 50, 140);
}
<label>Adjust so square below equals 1 inch:
<input type=range value=96 min=72 max=145></label>
<canvas width=600 height=300></canvas>
This example can of course be extended to take a vertical parameter as well as considering pixel aspect ratio (ie. retina displays) and scale.
You need to then build all your objects and graphics using a base scale, for example 96 DPI. Then use the relationship between the actual DPI and 96 DPI as a scale factor for all positions and sizes.

how to draw 2 centimeters long line on screen in adobe air

I need to draw 2 centimeters long line on screen on an Adobe Air application. I don't know how to do it!
Explanation:
I am getting parameters from another application say x centimeters, and that parameter is in centimeters.
I need to draw a circle exactly x centimeters from the top of the screen.
best regards
If I remember correctly, you won't be able to do it on desktop since AIR always returns 72DPI for the screen (I may be incorrect on that point, however). It is fairly easy to do on mobile, though, assuming AIR returns the proper DPI (retina iPads did not return the correct DPIs prior to AIR 3.3, I believe).
Basically, you convert inches to pixels simply by multiplying by the DPI.
var dpi:Number = Capabilities.screenDPI; //unnecessary to save local version, just easier to reference
var heightCM:Number = 5;
var widthCM:Number = 5;
var widthPixels:Number, heightPixels:Number;
var heightIn:Number = cmToInches( heightCM );
var widthIn:Number = cmToInches( widthCM );
widthPixels = widthIn * dpi;
heightPixels = heightIn * dpi;
function cmToInches( value:Number ):Number {
return value * .393701;
}
That will take a size (I built it for height and width, but you can adapt it to your needs) in centimeters, convert it to inches, and then convert it to pixels. You'd obviously want to turn that into a neat static Util method, but it would do the trick.
If you want, I created a Flex application last year to try and understand how AIR handles DPI differences. It just draws a red rectangle to a specific size on screen using on-screen sliders to determine the size (in inches). I don't have it here at work, but I could post the code when I get home.
Again, I do not believe this will work in desktop applications due to AIR always reporting 72 DPI. I hope I am wrong, but I do not believe I am.

Resources