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
0 commit comments