User Tools

Site Tools


Sidebar

Privacy Policy

News

Version 2.50 is out since Jan 1st 2022


Frequently Asked Questions


Namespaces

Note for contributors

If you wish to create a new page in the DroidScript wiki, please click on the most appropriate namespace above and follow the notes for contributors there.

Because of spam, it has been necessary to add a CAPTCHA to the registration form and the save option for editing pages. You will not need to prove you are human if you are logged in, so please register.

Please feel free to improve any existing page, as well as adding new pages to increase the sum of public knowledge about DroidScript.

Formatting Syntax

sample_code:image_array

This is an old revision of the document!


Image array

Click part of the image to store a number in a 2d array representing the rectangles of the image.

Click a rectangle multiple times and it will toggle between 1 and 2.

A colour-coded rectangle appears on the image to show what value was in the array before it was updated.

This is intended as a demo of one way to treat an image as a grid.

imageArray.js
var cellData=[];
//Called when application is started.
function OnStart()
{
    //Create a layout with objects vertically centered.
    var lay = app.CreateLayout( "linear", "VCenter,FillXY" );    
 
    //Create a text label and add it to layout.
    var img = app.CreateImage( null, 0.9, 0.5 );
    img.SetColor("#ff999999")
    img.SetOnTouch(imgOnTouch);
    
    lay.AddChild( img );
    var btnSave=app.CreateButton("Save");
    btnSave.SetOnTouch(btnSaveOnTouch);
    lay.AddChild(btnSave);
    //Add layout to app.    
    app.AddLayout( lay );
}
 
function imgOnTouch(ev)
{
     var row,col,val,x1,y1,x2,y2
     var cellWidth=0.1,cellHeight=0.1;
     if(ev.action==="Down")
     {
         row = Math.floor(ev.Y/cellHeight);
         col = Math.floor(ev.X/cellWidth);
         x1 = col*cellWidth;
         y1 = row*cellHeight;
         x2 = x1 + cellWidth;
         y2 = y1 + cellHeight;
         val = getData(row,col);
         this.SetLineWidth(1);
         this.SetPaintStyle("Fill")
         setData(row,col,(val+1)%2);
         if(val < 0)
         {
            this.SetPaintColor("#ffff0000");
            this.SetPaintStyle("Line")
            this.DrawRectangle(x1,y1,x2,y2);
         }
         else if(val === 0)
         {
            this.SetPaintColor("#ff00ff00");
            this.DrawRectangle(x1,y1,x2,y2);
         }
         else if(val === 1)
         {
            this.SetPaintColor("#ff0000ff");
            this.DrawRectangle(x1,y1,x2,y2);
         }
         app.ShowPopup(val);
     }
}
 
function getData(row,col)
{
   var ret;
   if (cellData.length < row +1)
      return -1;  // represents unset
   ret = cellData[row][col];
   if(ret===undefined)
      return -1;
   return ret;
}
function setData(row,col,value)
{
   while (cellData.length < row +1)
   {
       cellData.push([]);
   }
   cellData[row][col]=value;
}
 
function btnSaveOnTouch()
{
     
    var data = JSON.stringify(cellData);
    app.WriteFile("/sdcard/picData",data)     
}
sample_code/image_array.1443452774.txt.gz · Last modified: 2015/09/28 23:06 (external edit)