protected void hideStatusBar(boolean hide) { if (!hide || AndroidGame.getSDKVersion() < 11) { return; } View rootView = getWindow().getDecorView(); try { java.lang.reflect.Method m = View.class.getMethod("setSystemUiVisibility", int.class); if (AndroidGame.getSDKVersion() <= 13) m.invoke(rootView, 0x0); m.invoke(rootView, 0x1); } catch (Exception e) { } }
@Override protected void onResume() { AndroidGame.debugLog("onResume"); if (setting != null && setting.listener != null) { setting.listener.onResume(); } if (game != null) { game.onResume(); } if (gameView != null) { gameView.onResume(); } super.onResume(); }
@Override protected void onDestroy() { AndroidGame.debugLog("onDestroy"); if (setting != null && setting.listener != null) { setting.listener.onExit(); } for (File file : getCacheDir().listFiles()) { file.delete(); } if (game != null && game.assets != null) { game.assets.getNativeAudio().onDestroy(); game.onExit(); } super.onDestroy(); }
public static RectI getDeviceScreenSize(Context context, boolean useDeviceSize) { RectI rect = new RectI(); WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); android.view.Display display = windowManager.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); int widthPixels = metrics.widthPixels; int heightPixels = metrics.heightPixels; if (!useDeviceSize) { rect.width = widthPixels; rect.height = heightPixels; return rect; } int buildInt = AndroidGame.getSDKVersion(); if (buildInt >= 14 && buildInt < 17) try { widthPixels = (Integer) android.view.Display.class.getMethod("getRawWidth").invoke(display); heightPixels = (Integer) android.view.Display.class.getMethod("getRawHeight").invoke(display); } catch (Exception ignored) { } if (buildInt >= 17) try { android.graphics.Point realSize = new android.graphics.Point(); android.view.Display.class .getMethod("getRealSize", android.graphics.Point.class) .invoke(display, realSize); widthPixels = realSize.x; heightPixels = realSize.y; } catch (Exception ignored) { } rect.width = widthPixels; rect.height = heightPixels; return rect; }
public void run() { Rect dstRect = new Rect(); long startTime = System.nanoTime(); while (running) { if (!holder.getSurface().isValid()) { continue; } float deltaTime = (System.nanoTime() - startTime) / 10000000.000f; startTime = System.nanoTime(); if (deltaTime > 3.15) { deltaTime = (float) 3.15; } game.getCurrentScreen().update(deltaTime); game.getCurrentScreen().paint(deltaTime); Canvas canvas = holder.lockCanvas(); canvas.getClipBounds(dstRect); canvas.drawBitmap(frameBuffer, null, dstRect, null); holder.unlockCanvasAndPost(canvas); } }
public void addView(final View view, int w, int h, AndroidLocation location) { if (view == null) { return; } android.widget.RelativeLayout viewLayout = new android.widget.RelativeLayout(this); android.widget.RelativeLayout.LayoutParams relativeParams = AndroidGame.createRelativeLayout(location, w, h); viewLayout.addView(view, relativeParams); addView(viewLayout); }
public void setImmersiveMode(boolean use) { if (!use || AndroidGame.getSDKVersion() < 19) { return; } View view = getWindow().getDecorView(); try { java.lang.reflect.Method m = View.class.getMethod("setSystemUiVisibility", int.class); int code = 0x00000100 | 0x00000200 | 0x00000400 | 0x00000002 | 0x00000004 | 0x00001000; m.invoke(view, code); } catch (Exception e) { } }
public void setActionBarVisibility(boolean visible) { if (AndroidGame.isAndroidVersionHigher(11)) { try { java.lang.reflect.Method getBarMethod = Activity.class.getMethod("getActionBar"); Object actionBar = getBarMethod.invoke(this); if (actionBar != null) { java.lang.reflect.Method showHideMethod = actionBar.getClass().getMethod((visible) ? "show" : "hide"); showHideMethod.invoke(actionBar); } } catch (Exception ex) { ex.printStackTrace(); } } }
@Override public void onWindowFocusChanged(boolean hasFocus) { AndroidGame.debugLog("onWindowFocusChanged(" + hasFocus + ")"); if (setting != null) { setImmersiveMode(setting.useImmersiveMode); hideStatusBar(setting.hideStatusBar); } if (game != null && game.assets != null && game.assets._audio != null) { if (hasFocus) { game.assets.getNativeAudio().onResume(); } else { game.assets.getNativeAudio().onPause(); } } }
public void setFullScreen(boolean fullScreen) { Window win = getWindow(); if (AndroidGame.isAndroidVersionHigher(11)) { int flagHardwareAccelerated = 0x1000000; win.setFlags(flagHardwareAccelerated, flagHardwareAccelerated); } if (fullScreen) { win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); win.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); win.requestFeature(android.view.Window.FEATURE_NO_TITLE); } else { win.setFlags( WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); } }
public void run() { Rect dstRect = new Rect(); // create a new rectangle long startTime = System.nanoTime(); // gets the initial start time while (running) { if (!holder.getSurface().isValid()) continue; float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f; // gets the nanotime of the next frame, divide to get it in seconds startTime = System.nanoTime(); // gets the time for the next frame game.getCurrentScreen().update(deltaTime); game.getCurrentScreen().present(deltaTime); // call the update and present methods each frame Canvas canvas = holder.lockCanvas(); // lock the canvas canvas.getClipBounds(dstRect); // get the clip bounds canvas.drawBitmap(framebuffer, null, dstRect, null); // draw the framebuffer to the canvas holder.unlockCanvasAndPost(canvas); // unlock and update the surfaceholder } }
protected AndroidGame initialize() { if (game != null) { game.register(mainData.onScreen()); } return game; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Loon.self = this; Context context = getApplicationContext(); this.onMain(); LMode mode = setting.showMode; if (mode == null) { mode = LMode.Fill; } if (setting != null && setting.fullscreen) { this.setFullScreen(setting.fullscreen); } else { int windowFlags = makeWindowFlags(); getWindow().setFlags(windowFlags, windowFlags); } int width = setting.width; int height = setting.height; // 是否按比例缩放屏幕 if (setting.useRatioScaleFactor) { float scale = scaleFactor(); width *= scale; height *= scale; setting.width_zoom = width; setting.height_zoom = height; setting.updateScale(); mode = LMode.MaxRatio; // 若缩放值为无法实现的数值,则默认操作 } else if (setting.width_zoom <= 0 || setting.height_zoom <= 0) { updateViewSize(setting.landscape(), setting.width, setting.height, mode); width = this.maxWidth; height = this.maxHeight; setting.width_zoom = this.maxWidth; setting.height_zoom = this.maxHeight; setting.updateScale(); mode = LMode.Fill; } this.game = createGame(); this.gameView = new AndroidGameViewGL(context, game); this.handler = new Handler(); setContentView(mode, gameView, width, height); setRequestedOrientation(orientation()); createWakeLock(setting.useWakelock); hideStatusBar(setting.hideStatusBar); setImmersiveMode(setting.useImmersiveMode); if (setting.useImmersiveMode && AndroidGame.getSDKVersion() >= 19) { try { Class<?> vlistener = Class.forName("loon.android.AndroidVisibilityListener"); Object o = vlistener.newInstance(); java.lang.reflect.Method method = vlistener.getDeclaredMethod("createListener", AndroidBase.class); method.invoke(o, this); } catch (Exception e) { } } try { final int REQUIRED_CONFIG_CHANGES = android.content.pm.ActivityInfo.CONFIG_ORIENTATION | android.content.pm.ActivityInfo.CONFIG_KEYBOARD_HIDDEN; android.content.pm.ActivityInfo info = this.getPackageManager() .getActivityInfo(new android.content.ComponentName(context, this.getClass()), 0); if ((info.configChanges & REQUIRED_CONFIG_CHANGES) != REQUIRED_CONFIG_CHANGES) { new android.app.AlertDialog.Builder(this) .setMessage( "Loon Tip : Please add the following line to the Activity manifest .\n[configChanges=\"keyboardHidden|orientation\"]") .show(); } } catch (Exception e) { Log.w("Loon", "Cannot access game AndroidManifest.xml file !"); } }