Layout.SetOrientation( orient ) ?

365 views
Skip to first unread message

RuslanX

unread,
May 26, 2016, 10:58:05 AM5/26/16
to DroidScript
What Layout.SetOrientation( orient )  // “Portrait” or “Landscape” method do, how it works ..I tested but no effect ?

Steve Garman

unread,
May 26, 2016, 11:08:52 AM5/26/16
to DroidScript
It is the same as setting the CreateLayout option "Landscape"or"Portrait"

It can be useful if you want to change the layout in the OnConfig event.

https://groups.google.com/d/msg/androidscript/8WayCkiNP0M/z5i5NCdoBgAJ

RuslanX

unread,
May 27, 2016, 3:48:59 AM5/27/16
to DroidScript
Sorry, but I can't understand how it works, can you show a sample ?

//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" );
   
}
}


RuslanX

unread,
May 27, 2016, 3:50:39 AM5/27/16
to DroidScript
It can be useful if you want to change the layout in the OnConfig event. - yes, this I want ..

Steve Garman

unread,
May 27, 2016, 4:09:13 AM5/27/16
to DroidScript

//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";
if( test=="Landscape") hv= "Horizontal";
lay.SetOrientation( hv );

txt = app.CreateText( "Hello " );
txt.SetTextSize( 64);
lay.AddChild( txt );

txt1 = app.CreateText( "Goodbye" );
txt1.SetTextSize( 64 );
lay.AddChild( txt1 );
app.AddLayout( lay );
}
//Called when screen rotates
function OnConfig()
{
var test = app.GetOrientation();
var hv = "Vertical";
if( test=="Landscape") hv= "Horizontal";
lay.SetOrientation( hv );
}

RuslanX

unread,
May 27, 2016, 5:16:16 AM5/27/16
to DroidScript
OK, I understood ... I thought that I can set always layer in 'Portrait' or 'Landscape' mode regardless of the orientation.

RuslanX

unread,
May 27, 2016, 6:08:05 AM5/27/16
to DroidScript
Steve, Can you help me ?  My app start in Landscape mode, but I need on rotation change view of image to fit display propotional,  look here sample:

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 a simple way to do this ?

Steve Garman

unread,
May 27, 2016, 9:41:01 AM5/27/16
to DroidScript
I would have thought the code I posted was much simpler.

Is there some reason you are avoiding OnConfig ?

Steve Garman

unread,
May 27, 2016, 10:18:47 AM5/27/16
to DroidScript
//Is this what you are trying to achieve?

//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 );
}

RuslanX

unread,
May 27, 2016, 10:32:36 AM5/27/16
to DroidScript
OnConfig() not work with app.SetOrientation(), so I use sensor to detect Orientation, I asking how easily to fit an image (can be different resolution) to fit display on different display orientation, resize images proportionally (or scaling an image to fit on layer, display..) ?

RuslanX

unread,
May 27, 2016, 10:39:52 AM5/27/16
to DroidScript
OK ... I want to rotate my phone and only some image-controls are rotated in dependense of orientation ,but all this without rotating layer, with app.SetOrientation("Landscaspe") ... so now I have an Image (random) what must be scaled and rotated to fit display and orientation .. is a way to do this easy ?

Steve Garman

unread,
May 27, 2016, 10:50:09 AM5/27/16
to DroidScript
By using app.SetOrientation, you are killing almost all the ways DroidScript can help you with the calculations.

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.

RuslanX

unread,
May 27, 2016, 3:08:36 PM5/27/16
to DroidScript
Steve, had one problem can not Rotate image correctly, look code:

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();
}

if I set angle = 0; //-90; to -90 have problem ...

RuslanX

unread,
May 28, 2016, 6:53:11 AM5/28/16
to DroidScript
Any body ?
Reply all
Reply to author
Forward
0 new messages