It can be useful if you want to change the layout in the OnConfig event.
https://groups.google.com/d/msg/androidscript/8WayCkiNP0M/z5i5NCdoBgAJ
//Called when application is started.
function OnStart()
{
//app.SetScreenMode("Game");
CurrentOrientation = app.GetOrientation();
//app.SetOrientation("Portrait", InitialiseLayout);
InitialiseLayout();
}
//Called when application is started.
function InitialiseLayout()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "Linear", "Landscape,VCenter,FillXY" );
lay.SetBackColor("#6666ff");
img1 = app.CreateImage( null, 0.8, 0.8 );
img1.SetBackColor("#ff6666");
img1.index = 0;
img1.SetOnTouchUp( btn_OnTouch );
lay.AddChild( img1 );
app.AddLayout( lay );
}
function btn_OnTouch()
{
if(img1.index === 0) {
img1.index = 1;
lay.SetOrientation( "Landscape" );
app.ShowPopup( "Landscape" );
}
else {
img1.index = 0;
lay.SetOrientation( "Portrait" );
app.ShowPopup( "Portrait" );
}
}var CurrentOrientation;
//Called when application is started.
function OnStart()
{
app.SetOrientation( "Landscape", InitialiseLayout );
}
function InitialiseLayout() {
// Init Sensor
InitializeRotationSensor();
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
lay.SetOrientation( "Landscape" );
//Create image control for previewing pictures.
img = app.CreateImage( "/Sys/Img/Droid1.png", 0.4, -1 );
img.SetBackColor( "#996666" );
lay.AddChild( img );
app.AddLayout( lay );
}
function InitializeRotationSensor() {
//Create and start sensor object in Orientation mode.
orient = app.CreateSensor("Orientation", "Medium");
orient.SetOnChange(sns_OnChange);
orient.SetMinChange(1);
orient.Start();
}
function StopRotationSensor() {
orient.Stop();
}
function StartRotationSensor() {
orient.Start();
}
function sns_OnChange(x, y, z, time) {
var result;
try {
if (z < -45) result = "Landscape";
else if (z > -25 && z < 25 && y > 40 && y < 130) result = "Portrait";
if (result !== CurrentOrientation && result !== undefined) {
CurrentOrientation = result;
ChangeOrientation();
}
} catch (e) { app.ShowPopup("Error: " + e); }
}
function ChangeOrientation() {
if(orient) StopRotationSensor();
if (CurrentOrientation == "Portrait") {
app.ShowPopup("Portrait", "Bottom, Short");
lay.SetOrientation( "Vertical" );
img.Rotate(-90);
} else if (CurrentOrientation == "Landscape") {
app.ShowPopup("Landscape", "Bottom, Short");
img.Rotate(0);
lay.SetOrientation( "Horizontal" );
}
StartRotationSensor();
}Is there some reason you are avoiding OnConfig ?
//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
var test = app.GetOrientation();
var hv = "Vertical";
var h= 0.4,w=-1;
if( test=="Landscape")
{
hv= "Horizontal";
h=-1;
w=0.4;
}
lay.SetOrientation( hv );
//Create image control for previewing pictures.
img = app.CreateImage( "/Sys/Img/Droid1.png", w,h );
img.SetBackColor( "#996666" );
lay.AddChild( img );
app.AddLayout( lay );
}
The way you are doing it is probably the best approach.
You may want to read the image from the file into an invisible image control to get the original width and height, so you can calculate the proportions for the size of the visible image control.
var CurrentOrientation, DisplayWidth, DisplayHeight;
function OnStart()
{
app.SetScreenMode("Game");
DisplayWidth = app.GetDisplayWidth();
DisplayHeight = app.GetDisplayHeight();
app.SetOrientation( "Landscape", InitialiseLayout );
}
function InitialiseLayout()
{
lay1 = app.CreateLayout( "linear", "VCenter,FillXY" );
//Create a text label and add it to layout.
img1 = app.CreateImage( "/Sys/Img/Droid1.png" );
img1.SetVisibility( "Hide" );
lay1.AddChild( img1 );
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
img2 = app.CreateImage(null,1,1);
lay.AddChild(img2);
//Add layout to app.
app.AddLayout( lay );
app.AddLayout( lay1 );
InitializeRotationSensor();
}
function resizeKeepingRatio(width, height, destWidth, destHeight)
{
if (!width || !height || width <= 0 || height <= 0)
{
throw "Params error";
}
var ratioW = width / destWidth;
var ratioH = height / destHeight;
if (ratioW <= 1 && ratioH <= 1)
{
var ratio = 1 / ((ratioW > ratioH) ? ratioW : ratioH);
width *= ratio;
height *= ratio;
}
else if (ratioW > 1 && ratioH <= 1)
{
var ratio = 1 / ratioW;
width *= ratio;
height *= ratio;
}
else if (ratioW <= 1 && ratioH > 1)
{
var ratio = 1 / ratioH;
width *= ratio;
height *= ratio;
}
else if (ratioW >= 1 && ratioH >= 1)
{
var ratio = 1 / ((ratioW > ratioH) ? ratioW : ratioH);
width *= ratio;
height *= ratio;
}
return {
width : width,
height : height
};
}
function InitializeRotationSensor() {
//Create and start sensor object in Orientation mode.
orient = app.CreateSensor("Orientation", "Medium");
orient.SetOnChange(sns_OnChange);
orient.SetMinChange(1);
orient.Start();
}
function StopRotationSensor() {
orient.Stop();
}
function StartRotationSensor() {
orient.Start();
}
function sns_OnChange(x, y, z, time) {
var result;
try {
if (z < -45) result = "Landscape";
else if (z > -25 && z < 25 && y > 40 && y < 130) result = "Portrait";
if (result !== CurrentOrientation && result !== undefined) {
CurrentOrientation = result;
ChangeOrientation();
}
} catch (e) { app.ShowPopup("Error: " + e); }
}
function ChangeOrientation() {
var w, h, dW, dH, angle;
if(orient) StopRotationSensor();
w = img1.GetAbsWidth() ;
h = img1.GetAbsHeight();
if (CurrentOrientation == "Portrait") {
lay.SetOrientation( "Vertical" );
dW = DisplayHeight;
dH = DisplayWidth;
angle = 0; //-90;
} else if (CurrentOrientation == "Landscape") {
lay.SetOrientation( "Horizontal" );
dW = DisplayWidth;
dH = DisplayHeight;
angle = 0;
}
var zzz = new resizeKeepingRatio(w, h, dW, dH);
app.ShowPopup(dW + " / " + dH);
//img2.Clear();
img2.SetSize(zzz.width, zzz.height, "px");
img2.DrawImage(img1,0,0,1,1, angle);
StartRotationSensor();
}angle = 0; //-90; to -90 have problem ...