Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit 78a65dd

Browse files
committed
initial additions
1 parent efc34b0 commit 78a65dd

File tree

7 files changed

+224
-0
lines changed

7 files changed

+224
-0
lines changed

MapboxAndroidDemo/src/main/AndroidManifest.xml

+7
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,13 @@
480480
android:name="android.support.PARENT_ACTIVITY"
481481
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
482482
</activity>
483+
<activity
484+
android:name=".examples.dds.AnimatedDashLineActivity"
485+
android:label="@string/activity_dds_animated_dash_line_title">
486+
<meta-data
487+
android:name="android.support.PARENT_ACTIVITY"
488+
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
489+
</activity>
483490
<activity
484491
android:name=".examples.dds.StyleLineIdentityPropertyActivity"
485492
android:label="@string/activity_dds_style_line_identity_property_title">

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.mapbox.mapboxandroiddemo.examples.camera.BoundingBoxCameraActivity;
4444
import com.mapbox.mapboxandroiddemo.examples.camera.RestrictCameraActivity;
4545
import com.mapbox.mapboxandroiddemo.examples.dds.AddRainFallStyleActivity;
46+
import com.mapbox.mapboxandroiddemo.examples.dds.AnimatedDashLineActivity;
4647
import com.mapbox.mapboxandroiddemo.examples.dds.BathymetryActivity;
4748
import com.mapbox.mapboxandroiddemo.examples.dds.ChoroplethJsonVectorMixActivity;
4849
import com.mapbox.mapboxandroiddemo.examples.dds.ChoroplethZoomChangeActivity;
@@ -785,6 +786,12 @@ private void listItems(int id) {
785786
new Intent(MainActivity.this, AddRainFallStyleActivity.class),
786787
R.string.activity_dds_time_lapse_rainfall_url, false, BuildConfig.MIN_SDK_VERSION));
787788

789+
exampleItemModels.add(new ExampleItemModel(
790+
R.string.activity_dds_animated_dash_line_title,
791+
R.string.activity_dds_animated_dash_line_description,
792+
new Intent(MainActivity.this, AnimatedDashLineActivity.class),
793+
R.string.activity_dds_animated_dash_line_url, false, BuildConfig.MIN_SDK_VERSION));
794+
788795
currentCategory = R.id.nav_dds;
789796
break;
790797
default:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package com.mapbox.mapboxandroiddemo.examples.dds;
2+
3+
// #-code-snippet: animated-dash-line full-java
4+
5+
import android.graphics.Color;
6+
import android.os.Bundle;
7+
import android.os.Handler;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.util.Log;
10+
11+
import com.mapbox.mapboxandroiddemo.R;
12+
import com.mapbox.mapboxsdk.Mapbox;
13+
import com.mapbox.mapboxsdk.maps.MapView;
14+
import com.mapbox.mapboxsdk.maps.MapboxMap;
15+
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
16+
import com.mapbox.mapboxsdk.style.layers.LineLayer;
17+
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
18+
19+
import java.net.MalformedURLException;
20+
import java.net.URL;
21+
22+
import static com.mapbox.mapboxsdk.style.layers.Property.LINE_CAP_ROUND;
23+
import static com.mapbox.mapboxsdk.style.layers.Property.LINE_JOIN_ROUND;
24+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap;
25+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
26+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineDasharray;
27+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;
28+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
29+
30+
/**
31+
* Create an effect of animated and moving LineLayer dashes by rapidly adjusting the
32+
* dash and gap lengths.
33+
*/
34+
public class AnimatedDashLineActivity extends AppCompatActivity implements OnMapReadyCallback {
35+
36+
private MapView mapView;
37+
private MapboxMap mapboxMap;
38+
private Handler handler;
39+
private String tag = "AnimatedDashLine";
40+
private RefreshDashAndGapRunnable refreshDashAndGapRunnable;
41+
private int animationSpeedMillseconds = 50;
42+
43+
@Override
44+
protected void onCreate(Bundle savedInstanceState) {
45+
super.onCreate(savedInstanceState);
46+
47+
// Mapbox access token is configured here. This needs to be called either in your application
48+
// object or in the same activity which contains the mapview.
49+
Mapbox.getInstance(this, getString(R.string.access_token));
50+
51+
// This contains the MapView in XML and needs to be called after the access token is configured.
52+
setContentView(R.layout.activity_animated_dash_line);
53+
54+
handler = new Handler();
55+
mapView = findViewById(R.id.mapView);
56+
mapView.onCreate(savedInstanceState);
57+
mapView.getMapAsync(this);
58+
}
59+
60+
@Override
61+
public void onMapReady(MapboxMap mapboxMap) {
62+
AnimatedDashLineActivity.this.mapboxMap = mapboxMap;
63+
initBikePathLayer();
64+
Log.d(tag, "onMapReady: here 1");
65+
Runnable runnable = new RefreshDashAndGapRunnable();
66+
Log.d(tag, "onMapReady: runnable made");
67+
handler.postDelayed(runnable, animationSpeedMillseconds);
68+
Log.d(tag, "onMapReady: here 2");
69+
}
70+
71+
private void initBikePathLayer() {
72+
try {
73+
GeoJsonSource geoJsonSource = new GeoJsonSource("animated_line_source", new URL(
74+
"https://raw.githubusercontent.com/Chicago/osd-bike-routes/master/data/Bikeroutes.geojson"
75+
));
76+
mapboxMap.addSource(geoJsonSource);
77+
LineLayer animatedDashBikeLineLayer = new LineLayer("animated_line_layer_id", "animated_line_source");
78+
animatedDashBikeLineLayer.withProperties(
79+
lineWidth(4.5f),
80+
lineColor(Color.parseColor("#bf42f4")),
81+
lineCap(LINE_CAP_ROUND),
82+
lineJoin(LINE_JOIN_ROUND)
83+
);
84+
mapboxMap.addLayer(animatedDashBikeLineLayer);
85+
} catch (MalformedURLException malformedUrlException) {
86+
Log.d("AnimatedDashLine", "Check the URL: " + malformedUrlException.getMessage());
87+
}
88+
}
89+
90+
private class RefreshDashAndGapRunnable implements Runnable {
91+
92+
private float valueOne, valueTwo, valueThree, valueFour, ValueFive;
93+
private float dashLength = 1;
94+
private float gapLength = 3;
95+
96+
// We divide the animation up into 40 totalNumberOfSteps to make careful use of the finite space in
97+
// LineAtlas
98+
private float totalNumberOfSteps = 40;
99+
100+
// A # of totalNumberOfSteps proportional to the dashLength are devoted to manipulating the dash
101+
private float dashSteps = totalNumberOfSteps * dashLength / (gapLength + dashLength);
102+
103+
// A # of totalNumberOfSteps proportional to the gapLength are devoted to manipulating the gap
104+
private float gapSteps = totalNumberOfSteps - dashSteps;
105+
106+
// The current currentStep #
107+
private int currentStep = 0;
108+
109+
private String TAG = "AnimatedDashLine";
110+
111+
@Override
112+
public void run() {
113+
Log.d(TAG, "RefreshDashAndGapRunnable run: ");
114+
currentStep = currentStep + 1;
115+
if (currentStep >= totalNumberOfSteps) {
116+
currentStep = 0;
117+
}
118+
if (currentStep < dashSteps) {
119+
valueOne = currentStep / dashSteps;
120+
valueTwo = (1 - valueOne) * dashLength;
121+
valueThree = gapLength;
122+
valueFour = valueOne * dashLength;
123+
ValueFive = 0;
124+
} else {
125+
valueOne = (currentStep - dashSteps) / (gapSteps);
126+
valueTwo = 0;
127+
valueThree = (1 - valueOne) * gapLength;
128+
valueFour = dashLength;
129+
ValueFive = valueOne * gapLength;
130+
}
131+
Log.d(TAG, "RefreshDashAndGapRunnable run: here");
132+
133+
Float[] newFloatArray = new Float[] {valueTwo, valueThree, valueFour, ValueFive};
134+
135+
mapboxMap.getLayer("animated_line_layer_id").setProperties(
136+
lineDasharray(newFloatArray));
137+
Log.d(TAG, "RefreshDashAndGapRunnable run: layer done being gotten");
138+
handler.postDelayed(this, animationSpeedMillseconds);
139+
}
140+
}
141+
142+
// Add the mapView lifecycle to the activity's lifecycle methods
143+
@Override
144+
public void onResume() {
145+
super.onResume();
146+
mapView.onResume();
147+
}
148+
149+
@Override
150+
protected void onStart() {
151+
super.onStart();
152+
mapView.onStart();
153+
}
154+
155+
@Override
156+
protected void onStop() {
157+
super.onStop();
158+
mapView.onStop();
159+
}
160+
161+
@Override
162+
public void onPause() {
163+
super.onPause();
164+
mapView.onPause();
165+
}
166+
167+
@Override
168+
public void onLowMemory() {
169+
super.onLowMemory();
170+
mapView.onLowMemory();
171+
}
172+
173+
@Override
174+
protected void onDestroy() {
175+
super.onDestroy();
176+
mapView.onDestroy();
177+
handler.removeCallbacks(refreshDashAndGapRunnable);
178+
refreshDashAndGapRunnable = null;
179+
handler = null;
180+
}
181+
182+
@Override
183+
protected void onSaveInstanceState(Bundle outState) {
184+
super.onSaveInstanceState(outState);
185+
mapView.onSaveInstanceState(outState);
186+
}
187+
}
188+
// #-end-code-snippet: animated-dash-line full-java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context=".examples.annotations.DrawMarkerActivity">
9+
10+
<com.mapbox.mapboxsdk.maps.MapView
11+
android:id="@+id/mapView"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
mapbox:mapbox_cameraTargetLat="41.87811"
15+
mapbox:mapbox_cameraTargetLng="-87.629798"
16+
mapbox:mapbox_styleUrl="@string/mapbox_style_dark"
17+
mapbox:mapbox_cameraZoom="13"/>
18+
19+
</FrameLayout>

MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<string name="activity_dds_multiple_geometries_description">Filter and draw multiple geometries from a single GeoJSON file.</string>
4444
<string name="activity_dds_bathymetry_description">Use data-driven styling to show bathymetry (water depth) data</string>
4545
<string name="activity_dds_info_window_symbol_layer_description">Use SymbolLayer and icons to show data in a BubbleLayout "info window".</string>
46+
<string name="activity_dds_animated_dash_line_description">Animated the gap size of a LineLayer for the appearance of moving lines.</string>
4647
<string name="activity_annotation_marker_description">Create a default marker with an InfoWindow.</string>
4748
<string name="activity_annotation_custom_marker_description">Create a marker with a custom icon using the Mapbox Maps SDK.</string>
4849
<string name="activity_annotation_geojson_line_description">Draw a polyline by parsing a GeoJSON file with the Mapbox Maps SDK.</string>

MapboxAndroidDemo/src/main/res/values/titles_strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<string name="activity_dds_bathymetry_title">Display water depth</string>
4444
<string name="activity_dds_info_window_symbol_layer_title">Symbol layer info window</string>
4545
<string name="activity_dds_image_clustering_title">SymbolLayer clustering</string>
46+
<string name="activity_dds_animated_dash_line_title">Animated line layer</string>
4647
<string name="activity_annotation_marker_title">Draw a marker</string>
4748
<string name="activity_annotation_custom_marker_title">Draw a custom marker icon</string>
4849
<string name="activity_annotation_geojson_line_title">Draw a GeoJSON line</string>

MapboxAndroidDemo/src/main/res/values/urls_strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<string name="activity_dds_bathymetry_url" translatable="false">https://i.imgur.com/mwhOcIR.png</string>
4545
<string name="activity_dds_info_window_symbol_layer_url" translatable="false">https://i.imgur.com/qYoqgDk.png</string>
4646
<string name="activity_dds_image_clustering_url" translatable="false">https://i.imgur.com/KjgNcS0.png</string>
47+
<string name="activity_dds_animated_dash_line_url" translatable="false">https://i.imgur.com/9JTOUQL.png</string>
4748
<string name="activity_annotation_marker_url" translatable="false">http://i.imgur.com/X59UoaY.png</string>
4849
<string name="activity_annotation_custom_marker_url" translatable="false">http://i.imgur.com/BIHtPwJ.png</string>
4950
<string name="activity_annotation_geojson_line_url" translatable="false">http://i.imgur.com/rIFjsrH.png</string>

0 commit comments

Comments
 (0)