Bitmap Class
Bitmap class manages all operations with pictures. Using this class developer can load images from files and get theirs size, configuration etc.Bitmap class supports following formats (*.jpg, *.png)
Example:
function loadImage()
{
// load bitmap file into memory
this.bitmap = Bitmap.fromFile("dat://moscrif.jpg");
}
...
// draw bitmap inside onDrawFunction
app.onDraw = function(sender, canvas)
{
canvas.drawBitmap(this.bitmap, 0, 0);
// there are also other functions for bitmap drawing see canvas class
}
Item Index
Methods
Methods
fromBytes
data
Loads image from byte array.
Parameters:
-
data
BytesByte array.
Returns:
Example:
function loadImage ()
{
const HOST = "moscrif.com";
const FILE = "/userfiles/moscrif2.png";
// variable for downloaded image
app.img = null;
var request = new WebClient();
request.open(HOST, 80, false, "");
request.onReceived = function(sender) {
// create bitmap from downloaded data
app.img = Bitmap.fromBytes(this.data);
app.invalidate();
}
request.getData(FILE);
}
….
app.onDraw = function(sender, canvas)
{
// draw image, if it has been downloaded
if(app.img)
canvas.drawBitmap(app.img, 0, 0);
}
fromFile
file
Function loads image from file. Supported types are:
- .jpg - format uses lossy compression but does not support alpha channel. Frequently used for digital photography.
- .png - bitmap format of image that employs lossless data compression. This format also support alpha channel.
Parameters:
-
file
StringFile name of a picture to be opened.
Returns:
Example:
this._img = Bitmap.fromFile("app://img.png");
...
canvas.drawBitmap(this._img, 0, 0);
fromRect
width
height
[opaque=false]
Function creates bitmap with specified dimension. Bitmap can be initially opaque.
Parameters:
Returns:
Example:
game.onStart = function()
{
// creates offscreen bitmap
this._bmp = Bitmap.fromRect(200, 100);
// creates canvas for the ofscreen bitmap
this._canvas = Canvas.fromBitmap(this._bmp);
// draws to bitmap - can be something more complex
this._canvas.drawLine(0, 0, 100, 100, new Paint());
}
game.onDraw = function(canvas)
{
canvas.clear(0xffffffff);
// draws the offscreen drawn bitmap
canvas.drawBitmap(this._bmp, 100, 100);
}
fromScreen
() Bitmap static
Function creates bitmap object from screen. THe bitmap is same as a current image on he screen.
Returns:
Example:
game.onPointerPressed = function()
{
// make screenshot
this._bitmap = Bitmap.fromScreen();
// save screen shot
this._bitmap.toFile("data://screenshot.jpg");
}
getSize
file
Returns size of image file in pixels without loading an image.
Parameters:
-
file
String
Returns:
Example:
// get image dimensions before loading
var (w, h) = Bitmap.getSize("app://img.png");
// w: 640 h: 960
console<<"w: "<<w<<" h: "<<h<<"\n";
reset
() chainable
Reset all values in object, included loaded image, height and width.
resize
param1
param2
Resizes bitmap according to required width and height.
Parameters:
Returns:
Example:
// resize bitmap
this._img = Bitmap.fromFile("app://img.png");
this._img = this._img.resize(System.width, System.height);
.......
canvas.drawBitmap(this._img, 0, 0);
toFile
file
Save bitmap to the image file. Supported types are:
- .jpg - format uses lossy compression but does not support alpha channel. Frequently used for digital photography.
- .png - bitmap format of image that employs lossless data compression. This format also support alpha channel.
Parameters:
-
file
StringName of a new image file.
Returns:
Example:
game.onPointerPressed = function()
{
// make screenshot
this._bitmap = Bitmap.fromScreen();
// save screen shot
this._bitmap.toFile("data://screenshot.jpg");
}
Properties
config
Integer
Returns a number representating bitmap's configuration. With this function, a developer can get a number of bits per pixel and also a color model.
- 0 - bitmap has not been configured
- 1 - 1-bit per pixel, (0 is transparent, 1 is opaque)
- 2 - 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque)
- 3 - 8-bits per pixel, using SkColorTable to specify the colors
- 4 - 16-bits per pixel (RGB)
- 5 - 16-bits per pixel (ARGB)
- 6 - 32-bits per pixel (ARGB)