Animate images with image overlay

View on GitHubSample viewer app

Animate a series of images with an image overlay.

Image of animate images with image overlay

Use case

An image overlay is useful for displaying fast and dynamic images; for example, rendering real-time sensor data captured from a drone. Each frame from the drone becomes a static image which is updated on the fly as the data is made available.

How to use the sample

The application loads a map of the Southwestern United States. Tap the "Start" or "Stop" button to toggle 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.

How it works

  1. Create an ImageOverlay and add it to the SceneView.
  2. Set up a timer with an initial interval time of 17ms, which will display approximately 60 ImageFrames per second.
  3. On every tick of the timer, add the next image frame to the image overlay.

Relevant API

  • ImageFrame
  • ImageOverlay
  • SceneView

About the data

These radar images were captured by the US National Weather Service (NWS). They highlight the Pacific Southwest sector which is made up of part the western United States and Mexico. For more information visit the National Weather Service website.

Additional information

The supported image formats are GeoTIFF, TIFF, JPEG, and PNG. ImageOverlay does not support the rich processing and rendering capabilities of a RasterLayer. Use Raster and RasterLayer for static image rendering, analysis, and persistence.

This sample uses the GeoViewCompose Toolkit module to implement a Composable MapView.

Tags

3d, animation, drone, dynamic, image frame, image overlay, real time, rendering

Sample Code

AnimateImagesWithImageOverlayViewModel.ktAnimateImagesWithImageOverlayViewModel.ktDownloadActivity.ktMainActivity.ktAnimateImagesWithImageOverlayScreen.kt
Use dark colors for code blocksCopy
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Copyright 2024 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.esri.arcgismaps.sample.animateimageswithimageoverlay.components

import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.geometry.Envelope
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISScene
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.view.Camera
import com.arcgismaps.mapping.view.ImageFrame
import com.arcgismaps.mapping.view.ImageOverlay
import com.esri.arcgismaps.sample.animateimageswithimageoverlay.R
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.io.File
import java.util.Timer
import kotlin.concurrent.fixedRateTimer

class AnimateImagesWithImageOverlayViewModel(application: Application) : AndroidViewModel(application) {

    private val provisionPath: String by lazy {
        application.getExternalFilesDir(null)?.path.toString() + File.separator + application.getString(
            R.string.animate_images_with_image_overlay_app_name
        )
    }

    // Get the folder path containing all the image overlays
    private val filePath = "$provisionPath/PacificSouthWest"

    var opacity by mutableFloatStateOf(1.0f)
        private set

    fun updateOpacity(opacityFromSlider: Float) {
        opacity = opacityFromSlider
        imageOverlay.opacity = opacity
    }

    val fpsOptions = listOf(60, 30, 15)
    private val _fps = MutableStateFlow(fpsOptions[1])
    val fps = _fps.asStateFlow()

    fun updateFpsOption(fpsIndex: Int) {
        _fps.value = fpsOptions[fpsIndex]
    }

    var isStarted by mutableStateOf(true)
        private set

    fun updateIsStarted(isStartedFromButton: Boolean) {
        isStarted = isStartedFromButton
        toggleAnimationTimer()
    }


    // Create an envelope of the pacific southwest sector for displaying the image frame
    private val pointForImageFrame = Point(
        x = -120.0724,
        y = 35.1310,
        spatialReference = SpatialReference.wgs84()
    )
    private val pacificSouthwestEnvelope = Envelope(
        center = pointForImageFrame,
        width = 15.0958,
        height = -14.3770
    )

    // Create a scene with the dark gray basemap and elevation source
    val arcGISScene by mutableStateOf(ArcGISScene(BasemapStyle.ArcGISDarkGray).apply {
        // Create a camera, looking at the pacific southwest sector
        val observationPoint = Point(-116.621, 24.7773, 856977.0)
        val camera = Camera(observationPoint, 353.994, 48.5495, 0.0)
        initialViewpoint = Viewpoint(pacificSouthwestEnvelope, camera)
    })

    // Create a message dialog view model for handling error messages
    val messageDialogVM = MessageDialogViewModel()

    // Keep track of the list of image frames added in cache
    private var imageFrames = mutableListOf<ImageFrame>()

    // Keep track of the image frame currently in view
    private var imageFrameIndex = 0

    // Timer task to customize frame rates
    private var timer: Timer? = null

    var imageOverlay = ImageOverlay()
        private set

    init {

        // Get the image files from local storage
        (File(filePath).listFiles())?.sorted()?.forEach { imageFile ->
            // Create an image with the given path and use it to create an image frame
            val imageFrame = ImageFrame(imageFile.path, pacificSouthwestEnvelope)
            imageFrames.add(imageFrame)
        }
        // Set the initial image frame to image overlay
        imageOverlay.imageFrame = imageFrames[imageFrameIndex]

        viewModelScope.launch {
            arcGISScene.load().onFailure { error ->
                messageDialogVM.showMessageDialog(
                    "Failed to load map",
                    error.message.toString()
                )
            }

            // On changes to the fps, create a new timer
            _fps.collect {
                if (isStarted) {
                    createNewTimer()
                }
            }
        }

        // Start the animation timer
        createNewTimer()
    }

    /**
     * Create a new image frame from the image at the current index and add it to the image overlay.
     */
    private fun addNextImageFrameToImageOverlay() {
        // Set image frame to image overlay
        imageOverlay.imageFrame = imageFrames[imageFrameIndex]
        // Increment the index to keep track of which image to load next
        imageFrameIndex++
        // Reset index once all files have been loaded
        if (imageFrameIndex == imageFrames.size)
            imageFrameIndex = 0
    }

    /**
     * Create a new timer for the given period which repeatedly calls [addNextImageFrameToImageOverlay]..
     */
    private fun createNewTimer() {
        // Get the current period from the fps state flow
        val period = when (_fps.value) {
            60 -> 17 // 1000ms/17 = 60 fps
            30 -> 33 // 1000ms/33 = 30 fps
            15 -> 67 // 1000ms/67 = 15 fps
            else -> 0
        }
        // Cancel any timers that might be running
        timer?.cancel()
        timer = null
        // Create a new timer with the given period
        timer = fixedRateTimer("Image overlay timer", period = period.toLong()) {
            addNextImageFrameToImageOverlay()
        }
    }

    /**
     * Toggles starting and stopping the timer on button tap.
     */
    private fun toggleAnimationTimer() {
        timer?.let {
            // Cancel any running timer
            timer?.cancel()
            timer = null
            // Change the start/stop button to "start"
            isStarted = false
        } ?: run {
            createNewTimer()
            // Change the start/stop button to "stop"
            isStarted = true
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.