User Tools

Site Tools


Privacy Policy


Version 2.50 is out since Jan 1st 2022

Frequently Asked Questions


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


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.

var cellData=[],img;
//Called when application is started.
function OnStart()
    //Create a layout with objects vertically centered.
    var lay = app.CreateLayout( "linear", "VCenter,FillXY" );    
    //Create image control and add it to layout.
    img = app.CreateImage( null, 0.9, 0.5 );
    lay.AddChild( img );
    // horizontal layout for buttons
    var hlay=app.CreateLayout("Linear","Horizontal");
    var btnSave=app.CreateButton("Save");
    var btnLoad=app.CreateButton("Load");
    var btnClear=app.CreateButton("Clear");
    var btnFresh=app.CreateButton("Refresh");
    //Add layout to app.    
    app.AddLayout( lay );
    //force initial load fro file
//user touched image - update cell
function imgOnTouch(ev)
     var row,col,val ;
         row = Math.floor(ev.Y/this.cellHeight);
         col = Math.floor(ev.X/this.cellWidth);
         val = getData(row,col);
         val = (val+1)%2;
//draw a single value to img
function drawCell(row,col)
    var val=getData(row,col)
    var x1 = col*img.cellWidth;
    var y1 = row*img.cellHeight;
    var x2 = x1 + img.cellWidth;
    var y2 = y1 + img.cellHeight;
    if(val < 0)
    else if(val === 0)
    else if(val === 1)
// draw all data to img
function populate()
    var row, cell, dataLen, rowLen, i, j;
    dataLen = cellData.length;
    for(i=0; i<dataLen ;i++) 
        row = cellData[i];
        rowLen = row.length;
        if(rowLen===0) rowLen=1 ;
// get a value from 2d array
function getData(row,col)
   var ret;
   if (cellData.length < row +1)
      return -1;  // represents unset
   ret = cellData[row][col];
      return -1;
   return ret;
//write a value to 2d array
function setData(row,col,value)
   // create any intervening rows
   while (cellData.length < row +1)
//save data to file
function btnSaveOnTouch()
    var data = JSON.stringify(cellData);
// get data from file and redraw
function btnLoadOnTouch()
    var data = app.ReadFile("/sdcard/picData");
    cellData = JSON.parse(data);
// clear data and redraw
function btnClearOnTouch()
    cellData = [];
sample_code/image_array.txt · Last modified: 2015/09/29 17:27 (external edit)