[Android] Add setPreferredFramesPerSecond for MapView (#13498)

* [Android] Add setPreferredFramesPerSecond for MapView
This commit is contained in:
Kevin Li
2018-12-12 09:15:55 +08:00
committed by GitHub
parent d812babecd
commit 2d527c2af7
5 changed files with 73 additions and 8 deletions

View File

@@ -421,6 +421,18 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
}
}
/**
* The maximum frame rate at which the map view is rendered,
* but it can't excess the ability of device hardware.
*
* @param maximumFps Can be set to arbitrary integer values.
*/
public void setMaximumFps(int maximumFps) {
if (mapRenderer != null) {
mapRenderer.setMaximumFps(maximumFps);
}
}
/**
* Returns if the map has been destroyed.
* <p>

View File

@@ -27,6 +27,7 @@ public abstract class MapRenderer implements MapRendererScheduler {
// Holds the pointer to the native peer after initialisation
private long nativePtr = 0;
private double expectedRenderTime = 0;
private MapboxMap.OnFpsChangedListener onFpsChangedListener;
public MapRenderer(@NonNull Context context, String localIdeographFontFamily) {
@@ -75,11 +76,20 @@ public abstract class MapRenderer implements MapRendererScheduler {
@CallSuper
protected void onDrawFrame(GL10 gl) {
long startTime = System.nanoTime();
try {
nativeRender();
} catch (java.lang.Error error) {
Logger.e(TAG, error.getMessage());
}
long renderTime = System.nanoTime() - startTime;
if (renderTime < expectedRenderTime) {
try {
Thread.sleep((long) ((expectedRenderTime - renderTime) / 1E6));
} catch (InterruptedException ex) {
Logger.e(TAG, ex.getMessage());
}
}
if (onFpsChangedListener != null) {
updateFps();
}
@@ -115,18 +125,26 @@ public abstract class MapRenderer implements MapRendererScheduler {
private native void nativeRender();
private long frames;
private long timeElapsed;
private void updateFps() {
frames++;
long currentTime = System.nanoTime();
double fps = 0;
if (currentTime - timeElapsed >= 1) {
fps = frames / ((currentTime - timeElapsed) / 1E9);
onFpsChangedListener.onFpsChanged(fps);
timeElapsed = currentTime;
frames = 0;
double fps = 1E9 / ((currentTime - timeElapsed));
onFpsChangedListener.onFpsChanged(fps);
timeElapsed = currentTime;
}
/**
* The max frame rate at which this render is rendered,
* but it can't excess the ability of device hardware.
*
* @param maximumFps Can be set to arbitrary integer values.
*/
public void setMaximumFps(int maximumFps) {
if (maximumFps <= 0) {
// Not valid, just return
return;
}
expectedRenderTime = 1E9 / maximumFps;
}
}

View File

@@ -63,6 +63,7 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa
setupMapView(savedInstanceState);
setupDebugChangeView();
setupStyleChangeView();
setupFpsChangeView();
}
private void setupToolbar() {
@@ -172,6 +173,15 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa
});
}
private void setupFpsChangeView() {
findViewById(R.id.fps_30).setOnClickListener(view -> {
mapView.setMaximumFps(30);
});
findViewById(R.id.fps_60).setOnClickListener(view -> {
mapView.setMaximumFps(60);
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return actionBarDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);

View File

@@ -71,6 +71,29 @@
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="bottom|end"/>
<Button
android:id="@+id/fps_60"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|start"
app:backgroundTint="@color/primary"
android:layout_margin="@dimen/fab_margin"
android:text="@string/fps60"
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="top|end"/>
<Button
android:id="@+id/fps_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_marginBottom="82dp"
app:backgroundTint="@color/primary"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin"
android:text="@string/fps30"
app:layout_anchor="@id/fps_60"
app:layout_anchorGravity="top"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView

View File

@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Mapbox Android SDK TestApp</string>
<string name="fps30">fps30</string>
<string name="fps60">fps60</string>
</resources>