Add fps mode for #646
This commit is contained in:
@@ -15,8 +15,10 @@ import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.mapbox.mapboxgl.lib.LonLatZoom;
|
||||
import com.mapbox.mapboxgl.lib.MapView;
|
||||
|
||||
public class MainActivity extends ActionBarActivity {
|
||||
|
||||
@@ -34,6 +36,9 @@ public class MainActivity extends ActionBarActivity {
|
||||
// Holds the MapFragment
|
||||
MapFragment mMapFragment;
|
||||
|
||||
// The FPS label
|
||||
TextView mFpsTextView;
|
||||
|
||||
//
|
||||
// Lifecycle events
|
||||
//
|
||||
@@ -47,6 +52,9 @@ public class MainActivity extends ActionBarActivity {
|
||||
// Load the layout
|
||||
setContentView(R.layout.activity_main);
|
||||
mMapFragment = (MapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map);
|
||||
mMapFragment.getMap().setOnFpsChangedListener(new MyOnFpsChangedListener());
|
||||
mFpsTextView = (TextView)findViewById(R.id.view_fps);
|
||||
mFpsTextView.setText("");
|
||||
|
||||
// Add a toolbar as the action bar
|
||||
Toolbar mainToolbar = (Toolbar)findViewById(R.id.toolbar_main);
|
||||
@@ -62,6 +70,10 @@ public class MainActivity extends ActionBarActivity {
|
||||
styleSpinner.setOnItemSelectedListener(new StyleSpinnerListener());
|
||||
}
|
||||
|
||||
//
|
||||
// Other events
|
||||
//
|
||||
|
||||
// Adds items to the action bar menu
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
@@ -70,9 +82,9 @@ public class MainActivity extends ActionBarActivity {
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
// Called when pressing action bar items
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
// Handle presses on the action bar items
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_gps:
|
||||
// Get a GPS position
|
||||
@@ -89,6 +101,14 @@ public class MainActivity extends ActionBarActivity {
|
||||
case R.id.action_debug:
|
||||
// Toggle debug mode
|
||||
mMapFragment.getMap().toggleDebug();
|
||||
|
||||
// Show the FPS counter
|
||||
if (mMapFragment.getMap().isDebugActive()) {
|
||||
mFpsTextView.setVisibility(View.VISIBLE);
|
||||
mFpsTextView.setText(getResources().getString(R.string.label_fps));
|
||||
} else {
|
||||
mFpsTextView.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
return true;
|
||||
|
||||
default:
|
||||
@@ -121,7 +141,7 @@ public class MainActivity extends ActionBarActivity {
|
||||
}
|
||||
}
|
||||
|
||||
// This class handles navigation events
|
||||
// This class handles style change events
|
||||
private class StyleSpinnerListener implements AdapterView.OnItemSelectedListener {
|
||||
|
||||
@Override
|
||||
@@ -168,4 +188,13 @@ public class MainActivity extends ActionBarActivity {
|
||||
mMapFragment.getMap().setStyleUrl("");
|
||||
}
|
||||
}
|
||||
|
||||
// Called when FPS changes
|
||||
public class MyOnFpsChangedListener implements MapView.OnFpsChangedListener {
|
||||
|
||||
@Override
|
||||
public void onFpsChanged(double fps) {
|
||||
mFpsTextView.setText(getResources().getString(R.string.label_fps) + String.format(" %4.2f", fps));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,16 +10,33 @@
|
||||
android:layout_width="match_parent"
|
||||
android:minHeight="?attr/actionBarSize"
|
||||
android:background="?attr/colorPrimary">
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/spinner_style"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
</android.support.v7.widget.Toolbar>
|
||||
|
||||
<fragment
|
||||
tools:context="${packageName}.${activityClass}"
|
||||
android:name="com.mapbox.mapboxgl.app.MapFragment"
|
||||
android:id="@+id/fragment_map"
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="fill_parent" />
|
||||
android:layout_height="fill_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<fragment
|
||||
android:name="com.mapbox.mapboxgl.app.MapFragment"
|
||||
android:id="@+id/fragment_map"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="${packageName}.${activityClass}"
|
||||
tools:layout="@layout/fragment_main" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/view_fps"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="?android:attr/textAppearanceLarge"
|
||||
android:text="@string/label_fps"
|
||||
android:padding="10dp" />
|
||||
|
||||
</FrameLayout>
|
||||
</LinearLayout>
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
<string name="action_debug">Toggle debug mode</string>
|
||||
|
||||
<string name="label_fps">FPS:</string>
|
||||
|
||||
<string-array name="style_list">
|
||||
<item>Bright</item>
|
||||
<item>Basic</item>
|
||||
|
||||
@@ -1177,7 +1177,36 @@ public class MapView extends SurfaceView {
|
||||
// Need to update anything that relies on map state
|
||||
protected void onMapChanged() {
|
||||
if (mOnMapChangedListener != null) {
|
||||
mOnMapChangedListener.onMapChanged();
|
||||
post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mOnMapChangedListener.onMapChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public interface OnFpsChangedListener {
|
||||
void onFpsChanged(double fps);
|
||||
}
|
||||
|
||||
private OnFpsChangedListener mOnFpsChangedListener;
|
||||
|
||||
// Adds a listener for onFpsChanged
|
||||
public void setOnFpsChangedListener(OnFpsChangedListener listener) {
|
||||
mOnFpsChangedListener = listener;
|
||||
}
|
||||
|
||||
// Called when debug mode is enabled to update a FPS counter
|
||||
// Called via JNI from NativeMapView
|
||||
protected void onFpsChanged(final double fps) {
|
||||
if (mOnFpsChangedListener != null) {
|
||||
post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mOnFpsChangedListener.onFpsChanged(fps);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -387,10 +387,13 @@ class NativeMapView {
|
||||
//
|
||||
|
||||
protected void onMapChanged() {
|
||||
//Log.v(TAG, "onMapChanged");
|
||||
mMapView.onMapChanged();
|
||||
}
|
||||
|
||||
protected void onFpsChanged(double fps) {
|
||||
mMapView.onFpsChanged(fps);
|
||||
}
|
||||
|
||||
//
|
||||
// JNI methods
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user