Skip to content

Commit e2d8f3a

Browse files
authored
cs/Update caching the ArcGIS images (#330)
* Switch using image array * preread image files. * optimize the application. * update the comments. * remove the speed control
1 parent 4c3f8be commit e2d8f3a

File tree

3 files changed

+16
-62
lines changed

3 files changed

+16
-62
lines changed

lib/samples/animate_images_with_image_overlay/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ An image overlay is useful for displaying fast and dynamic images; for example,
1010

1111
## How to use the sample
1212

13-
The application loads a map of the Southwestern United States. Tap the "Start" or "Stop" buttons to start or stop the radar animation. Use the drop down menu to select how quickly the animation plays. Move the slider to change the opacity of the image overlay.
13+
The application loads a map of the Southwestern United States. Tap the "Start" or "Stop" buttons to start or stop the radar animation. Move the slider to change the opacity of the image overlay.
1414

1515
## How it works
1616

1717
1. Create an `ImageOverlay` and add it to the `ArcGISSceneViewController`.
18-
2. Set up a timer with an initial interval time of 68ms, which will display approximately 15 `ImageFrame`s per second.
18+
2. Set up a ticker with the interval time of 68ms, which will display approximately 15 `ImageFrame`s per second.
1919
3. When `Start` is clicked, start the timer.
20-
4. The timer updates the `ImageFrame` of the `ImageOverlay` at the given time interval.
20+
4. The ticker updates the `ImageFrame` of the `ImageOverlay` at the given time interval.
2121

2222
## Relevant API
2323

lib/samples/animate_images_with_image_overlay/animate_images_with_image_overlay.dart

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,16 @@ class _AnimateImagesWithImageOverlayState
3737
final _sceneViewController = ArcGISSceneView.createController();
3838
// A flag to toggle the start/stop of the image animation.
3939
var _started = false;
40-
// Define the animated speeds available in the dropdown button.
41-
final _animatedSpeeds = ['Fast', 'Medium', 'Slow'];
42-
// The initial selected animated speed.
43-
var _selectedAnimatedSpeed = 'Slow';
4440
// The speed of the image frame animation in milliseconds.
45-
var _imageFrameSpeed = 0;
41+
final _imageFrameSpeed = 68;
4642
// The last frame time in milliseconds to control the animation speed.
4743
var _lastFrameTime = 0;
4844
// The initial opacity of the image overlay.
4945
var _opacity = 0.5;
5046
// Create an ImageOverlay to display the animated images.
5147
final _imageOverlay = ImageOverlay();
52-
// A list to hold the image URIs for the animation.
53-
List<Uri> _imageUriList = [];
48+
// A list to hold the ArcGIS image files for the animation.
49+
List<File> _imageFileList = [];
5450
// A string to display the download progress.
5551
var _downloadProgress = '';
5652
// An integer to track the current ArcGIS image index.
@@ -70,17 +66,11 @@ class _AnimateImagesWithImageOverlayState
7066
height: -14.3770441522488,
7167
);
7268

73-
@override
74-
void initState() {
75-
_imageFrameSpeed = getAnimatedSpeed(_selectedAnimatedSpeed);
76-
super.initState();
77-
}
78-
7969
@override
8070
void dispose() {
8171
_ticker?.dispose();
8272
_ticker = null;
83-
_imageUriList.clear();
73+
_imageFileList.clear();
8474
super.dispose();
8575
}
8676

@@ -119,29 +109,6 @@ class _AnimateImagesWithImageOverlayState
119109
? const Text('Stop')
120110
: const Text('Start'),
121111
),
122-
// A dropdown button to select the animated speed.
123-
DropdownButton<String>(
124-
value: _selectedAnimatedSpeed,
125-
onChanged: (value) {
126-
if (value != null) {
127-
setState(() {
128-
_selectedAnimatedSpeed = value;
129-
_imageFrameSpeed = getAnimatedSpeed(value);
130-
});
131-
}
132-
},
133-
items: _animatedSpeeds.map((String speed) {
134-
return DropdownMenuItem<String>(
135-
value: speed,
136-
child: Text(
137-
speed,
138-
style: TextStyle(
139-
color: Theme.of(context).primaryColor,
140-
),
141-
),
142-
);
143-
}).toList(),
144-
),
145112
],
146113
),
147114
// A Slider to control opacity of the image overlay.
@@ -179,22 +146,13 @@ class _AnimateImagesWithImageOverlayState
179146
);
180147
}
181148

182-
int getAnimatedSpeed(String selectedSpeed) {
183-
// Returns the speed of the animation based on the selected speed.
184-
// Usually a frame changes every 16 milliseconds.
185-
return switch (selectedSpeed) {
186-
'Fast' => 17,
187-
'Medium' => 34,
188-
'Slow' => 68,
189-
_ => 68,
190-
};
191-
}
192-
149+
// Stop the image animation ticker.
193150
void stopTicker() {
194151
_ticker?.stop();
195152
setState(() => _started = false);
196153
}
197154

155+
// Start the image animation ticker.
198156
void startTicker() {
199157
_lastFrameTime = 0;
200158
// create a ticker to control the image frame animation.
@@ -207,7 +165,7 @@ class _AnimateImagesWithImageOverlayState
207165
void _onTicker(Duration elapsed) {
208166
final delta = elapsed.inMilliseconds - _lastFrameTime;
209167
if (delta >= _imageFrameSpeed) {
210-
_imageFrameIndex = (_imageFrameIndex + 1) % _imageUriList.length;
168+
_imageFrameIndex = (_imageFrameIndex + 1) % _imageFileList.length;
211169
setImageFrame(_imageFrameIndex);
212170
_lastFrameTime = elapsed.inMilliseconds;
213171
}
@@ -274,28 +232,24 @@ class _AnimateImagesWithImageOverlayState
274232
);
275233
}
276234
// Get a list of all PNG image files in the extracted directory.
277-
final imageFileList = directory
235+
_imageFileList = directory
278236
.listSync()
279237
.whereType<File>()
280238
.where((file) => file.path.endsWith('.png'))
281239
.toList();
282240
// Sort the list by file path name.
283-
imageFileList.sort((file1, file2) => file1.path.compareTo(file2.path));
284-
285-
// Generate the Uris from the image files.
286-
_imageUriList = imageFileList.map((file) {
287-
return file.uri;
288-
}).toList();
289-
241+
_imageFileList.sort((file1, file2) => file1.path.compareTo(file2.path));
242+
290243
// show the first image frame in the image overlay.
291244
setImageFrame(0);
292245
}
293246

294247
/// Sets the image frame to the image overlay based on the index.
295248
void setImageFrame(int index) {
296-
final image = ArcGISImage.fromFile(_imageUriList[index]);
249+
// Quickly release the previous image frame to avoid memory issues.
250+
_imageOverlay.imageFrame = null;
297251
_imageOverlay.imageFrame = ImageFrame.withImageEnvelope(
298-
image: image!,
252+
image: ArcGISImage.fromFile(_imageFileList[index].uri)!,
299253
extent: imageEnvelope,
300254
);
301255
}
2.2 KB
Loading

0 commit comments

Comments
 (0)