Transparent render surface
A newer version of the Maps SDK is available
This page uses v9.7.1 of the Mapbox Maps SDK. A newer version of the SDK is available. Learn about the latest version, v11.6.0, in the Maps SDK documentation.
Note
This example is a part of the Mapbox Android Demo app. You can find the values for all referenced resources in the res
directory. For example, see res/values/activity_strings.xml
for R.string.*
references used in this example. The dependencies can be found here.The examples use View binding.See setup documention if necessary.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:scaleX="1.8"
android:scaleY="1.8"
android:layout_height="match_parent"
android:layout_gravity="center" />
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="48.507879"
app:mapbox_cameraTargetLng="8.363795"
app:mapbox_cameraZoom="2"
app:mapbox_cameraZoomMin="1.3"
app:mapbox_renderTextureMode="true"
app:mapbox_renderTextureTranslucentSurface="true" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
package com.mapbox.mapboxandroiddemo.examples.styles;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.RawRes;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.VideoView;
import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import timber.log.Timber;
/**
* Create a transparent render surface and add whatever you want to the background. This example
* has a video of moving water behind Earth's land.
*/
public class TransparentBackgroundActivity extends AppCompatActivity implements OnMapReadyCallback {
private MapView mapView;
private VideoView backgroundWaterVideoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));
// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_style_transparent_render_background);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
try {
// Switch the map to a style that has no background
mapboxMap.setStyle(new Style.Builder().fromJson(readRawResource(this, R.raw.no_bg_style)),
new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
initVideoView();
}
});
} catch (IOException exception) {
Timber.e(exception);
}
}
/**
* Place the video of moving water behind the map
*/
private void initVideoView() {
backgroundWaterVideoView = findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.moving_background_water;
backgroundWaterVideoView.setVideoURI(Uri.parse(path));
backgroundWaterVideoView.start();
backgroundWaterVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
backgroundWaterVideoView.start();
}
});
}
// Add the mapView lifecycle to the activity's lifecycle methods
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
if (backgroundWaterVideoView != null) {
backgroundWaterVideoView.stopPlayback();
}
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* Get the map style JSON from the raw file in the app's raw folder
*/
public static String readRawResource(Context context, @RawRes int rawResource) throws IOException {
String json = "";
if (context != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try (InputStream is = context.getResources().openRawResource(rawResource)) {
Reader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
int numRead;
while ((numRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, numRead);
}
}
json = writer.toString();
}
return json;
}
}