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

Commit 3cceaea

Browse files
committed
initial additions
1 parent efc34b0 commit 3cceaea

File tree

7 files changed

+227
-0
lines changed

7 files changed

+227
-0
lines changed

MapboxAndroidDemo/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
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_styles_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

Lines changed: 7 additions & 0 deletions
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_styles_dds_animated_dash_line_title,
791+
R.string.activity_styles_dds_animated_dash_line_description,
792+
new Intent(MainActivity.this, AnimatedDashLineActivity.class),
793+
R.string.activity_styles_dds_animated_dash_line_url, false, BuildConfig.MIN_SDK_VERSION));
794+
788795
currentCategory = R.id.nav_dds;
789796
break;
790797
default:
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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+
41+
@Override
42+
protected void onCreate(Bundle savedInstanceState) {
43+
super.onCreate(savedInstanceState);
44+
45+
// Mapbox access token is configured here. This needs to be called either in your application
46+
// object or in the same activity which contains the mapview.
47+
Mapbox.getInstance(this, getString(R.string.access_token));
48+
49+
// This contains the MapView in XML and needs to be called after the access token is configured.
50+
setContentView(R.layout.activity_animated_dash_line);
51+
52+
mapView = findViewById(R.id.mapView);
53+
mapView.onCreate(savedInstanceState);
54+
mapView.getMapAsync(this);
55+
}
56+
57+
@Override
58+
public void onMapReady(MapboxMap mapboxMap) {
59+
AnimatedDashLineActivity.this.mapboxMap = mapboxMap;
60+
initBikePathLayer();
61+
Log.d(tag, "onMapReady: ");
62+
}
63+
64+
private void initBikePathLayer() {
65+
try {
66+
GeoJsonSource geoJsonSource = new GeoJsonSource("animated_line_source", new URL(
67+
"https://raw.githubusercontent.com/Chicago/osd-bike-routes/master/data/Bikeroutes.geojson"
68+
));
69+
mapboxMap.addSource(geoJsonSource);
70+
LineLayer animatedDashBikeLineLayer = new LineLayer("animated_line_layer_id", "animated_line_source");
71+
animatedDashBikeLineLayer.withProperties(
72+
lineWidth(4.5f),
73+
lineColor(Color.parseColor("#bf42f4")),
74+
lineCap(LINE_CAP_ROUND),
75+
lineJoin(LINE_JOIN_ROUND)
76+
);
77+
mapboxMap.addLayer(animatedDashBikeLineLayer);
78+
Log.d(tag, "initBikePathLayer: here");
79+
Runnable runnable = new RefreshDashAndGapRunnable(this.mapboxMap, new Handler());
80+
Log.d(tag, "initBikePathLayer: runnable made");
81+
handler.postDelayed(runnable, 25);
82+
Log.d(tag, "initBikePathLayer: postDelayed");
83+
} catch (MalformedURLException malformedUrlException) {
84+
Log.d("AnimatedDashLine", "Check the URL: " + malformedUrlException.getMessage());
85+
}
86+
}
87+
88+
private static class RefreshDashAndGapRunnable implements Runnable {
89+
90+
private float valueOne, valueTwo, valueThree, valueFour, ValueFive;
91+
private float dashLength = 1;
92+
private float gapLength = 3;
93+
94+
// We divide the animation up into 40 steps to make careful use of the finite space in
95+
// LineAtlas
96+
private float steps = 40;
97+
98+
// A # of steps proportional to the dashLength are devoted to manipulating the dash
99+
private float dashSteps = steps * dashLength / (gapLength + dashLength);
100+
101+
// A # of steps proportional to the gapLength are devoted to manipulating the gap
102+
private float gapSteps = steps - dashSteps;
103+
104+
// The current step #
105+
private int step = 0;
106+
107+
private MapboxMap mapboxMap;
108+
private Handler handler;
109+
private String TAG = "AnimatedDashLine";
110+
111+
RefreshDashAndGapRunnable(MapboxMap mapboxMap, Handler handler) {
112+
this.mapboxMap = mapboxMap;
113+
this.handler = handler;
114+
Log.d(TAG, "RefreshDashAndGapRunnable: finished");
115+
116+
mapboxMap.getProjection().getProjectedMetersForLatLng()
117+
}
118+
119+
@Override
120+
public void run() {
121+
Log.d(TAG, "run: ");
122+
step = step + 1;
123+
if (step >= steps) {
124+
step = 0;
125+
}
126+
if (step < dashSteps) {
127+
valueOne = step / dashSteps;
128+
valueTwo = (1 - valueOne) * dashLength;
129+
valueThree = gapLength;
130+
valueFour = valueOne * dashLength;
131+
ValueFive = 0;
132+
} else {
133+
valueOne = (step - dashSteps) / (gapSteps);
134+
valueTwo = 0;
135+
valueThree = (1 - valueOne) * gapLength;
136+
valueFour = dashLength;
137+
ValueFive = valueOne * gapLength;
138+
}
139+
Log.d(TAG, "run: here");
140+
mapboxMap.getLayer("animated_line_layer_id").setProperties(
141+
lineDasharray(new Float[] {valueTwo, valueThree, valueFour, ValueFive})
142+
);
143+
Log.d(TAG, "run: layer done being gotten");
144+
handler.postDelayed(this, 25);
145+
}
146+
}
147+
148+
// Add the mapView lifecycle to the activity's lifecycle methods
149+
@Override
150+
public void onResume() {
151+
super.onResume();
152+
mapView.onResume();
153+
}
154+
155+
@Override
156+
protected void onStart() {
157+
super.onStart();
158+
mapView.onStart();
159+
}
160+
161+
@Override
162+
protected void onStop() {
163+
super.onStop();
164+
mapView.onStop();
165+
}
166+
167+
@Override
168+
public void onPause() {
169+
super.onPause();
170+
mapView.onPause();
171+
}
172+
173+
@Override
174+
public void onLowMemory() {
175+
super.onLowMemory();
176+
mapView.onLowMemory();
177+
}
178+
179+
@Override
180+
protected void onDestroy() {
181+
super.onDestroy();
182+
mapView.onDestroy();
183+
}
184+
185+
@Override
186+
protected void onSaveInstanceState(Bundle outState) {
187+
super.onSaveInstanceState(outState);
188+
mapView.onSaveInstanceState(outState);
189+
}
190+
}
191+
// #-end-code-snippet: animated-dash-line full-java
Lines changed: 19 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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_styles_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

Lines changed: 1 addition & 0 deletions
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_styles_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

Lines changed: 1 addition & 0 deletions
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_styles_dds_animated_dash_line_url" translatable="false">https://i.imgur.com/KjgNcS0.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)