Standard image loading waits for completion when requested, but asynchronous loading allows other processing to occur while the image is loaded in the background. This makes it possible to design the application so that slow disk reading operations are less noticeable.
Specifications
- Asynchronous loading is performed using Bitmap.loadAsync(filename) and Bitmap.onLoaded(meta,async,error,message).
- Request loading with loadAsync and receive completion notification with onLoaded.
- Whether an asynchronous load is in progress can be determined with Bitmap.loading.
- Accessing other members of the Bitmap during asynchronous loading will cause an exception.
- Note that the filename argument for loadAsync must include the extension, unlike Layer.loadImages where it can be omitted.
The values received in onLoaded (meta, async, error, message) are as follows:
- meta : Same as the return value of Layer.loadImages. A dictionary array of tag information.
- async : Whether it was loaded asynchronously. If it was in the cache, it returns via onLoaded immediately after the load request.
- error : Whether a loading error occurred.
- message : Error message. If an error occurs, the error message is passed.
Loaded images can be copied from the Bitmap to a Layer using Layer.copyFromBitmapToMainImage(Bitmap). Although called a copy, the data is shared until modified, so it completes instantly. Since the loading process is asynchronous, the Layer to which the image is passed might already be invalidated by the time loading completes. When accessing other objects in onLoaded, it is better to check if they have been invalidated. Alternatively, you must ensure they are not invalidated until onLoaded completes.
Sample Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Asynchronously load image0.png to image9.png 10 times
*/
System.setArgument("-contfreq", 60);
System.graphicCacheLimit = 0;
// Disable image cache as it is meaningless if the cache is active
class Bitmap2Layer extends Bitmap {
var target;
var tag;
function Bitmap2Layer(layer,filename) {
super.Bitmap();
this.target = layer;
this.tag = filename;
}
function onLoaded(meta,async,error,message) {
Debug.message("Exit load async:"+async+", error:"+error+", message:"+message);
if( !error && isvalid(target) ) { // Since it is asynchronous, check that it has not already been invalidated
target.copyFromBitmapToMainImage(this);
target.setSizeToImageSize();
}
}
};
class MainWindow extends Window {
var base;
var layer;
var layermove;
var addval;
var bmps;
function MainWindow( width, height ) {
super.Window();
setSize( width, height );
setInnerSize( width, height );
base = new Layer(this, null);
base.setSize(width,height);
base.setSizeToImageSize();
base.name = "base";
base.visible = true;
add( base );
layer = new Layer(this,base);
layer.setSize(100,100);
layer.setSizeToImageSize();
layer.colorRect(0,0,100,100,0x00ff00,128);
layer.visible = true;
add( layer );
layermove = new Layer(this,base);
layermove.setSize(100,100);
layermove.setPos(0,height-100);
layermove.setSizeToImageSize();
layermove.colorRect(0,0,100,100,0xff0000,128);
layermove.drawText( 0, 40, "Test string rendering", 0xffffff );
layermove.visible = true;
add( layermove );
bmps = new Array();
for( var j = 0; j < 10; j++ ) {
for( var i = 0; i < 10; i++ ) {
var filename = "image"+i+".png";
var bmp = new Bitmap2Layer(layer,filename);
bmp.loadAsync(filename);
bmps.add(bmp);
}
}
add( bmps );
addval = 1;
System.addContinuousHandler(onMoveImage);
}
function finalize() {
System.removeContinuousHandler(onMoveImage);
super.finalize();
}
function onMoveImage() {
layermove.left += addval;
if( addval > 0 ) {
if( layermove.left >= (this.width-layermove.width) ) {
addval = -1;
}
} else {
if( layermove.left <= 0 ) {
addval = 1;
}
}
}
};
var win = new MainWindow(640,480);
win.visible = true;