Transparent background
This example demonstrates using a custom video as a background for a MapView in the Mapbox Maps SDK for Android. By leveraging the texture_view
rendering for the MapView
, the background video can have alpha channel translucency. The TransparentBackgroundActivity
class extends AppCompatActivity
and includes functionality to load a custom video (moving_background_water
) as the background video.
In the onCreate
method, the custom JSON style containing map layer information is loaded using mapboxMap.loadStyle
. This method initializes the background video view (videoView
) by setting the video URI and starting playback. When the video playback completes, the app automatically sets it to replay. The onDestroy
method ensures the video playback is stopped when the activity is destroyed.
This example code is part of the Maps SDK for Android Examples App, a working Android project available on GitHub. Android developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.
See our Run the Maps SDK for Android Examples App tutorial for step-by-step instructions.
package com.mapbox.maps.testapp.examples
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.databinding.ActivityTransparentBackgroundBinding
/**
* Example of using custom video as a background for MapView.
* This is possible when using `texture_view` rendering for MapView because it supports alpha channel translucency.
*/
class TransparentBackgroundActivity : AppCompatActivity() {
private lateinit var binding: ActivityTransparentBackgroundBinding
private fun initVideoView() {
val path = "android.resource://" + packageName + "/" + R.raw.moving_background_water
binding.videoView.apply {
setVideoURI(Uri.parse(path))
start()
setOnCompletionListener { binding.videoView.start() }
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityTransparentBackgroundBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.mapView.mapboxMap.loadStyle(
"""
{
"version": 8,
"name": "Land",
"metadata": {
"mapbox:autocomposite": true
},
"sources": {
"composite": {
"url": "mapbox://mapbox.mapbox-terrain-v2",
"type": "vector"
}
},
"glyphs": "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
"layers": [
{
"layout": {
"visibility": "visible"
},
"type": "fill",
"source": "composite",
"id": "admin",
"paint": {
"fill-color": "hsl(359, 100%, 50%)",
"fill-opacity": 1
},
"source-layer": "landcover"
},
{
"layout": {
"visibility": "visible"
},
"type": "fill",
"source": "composite",
"id": "layer-0",
"paint": {
"fill-opacity": 1,
"fill-color": "hsl(359, 100%, 50%)"
},
"source-layer": "Layer_0"
}
]
}
""".trimIndent()
) { initVideoView() }
}
override fun onDestroy() {
super.onDestroy()
binding.videoView.stopPlayback()
}
}