Android实现监听屏幕旋转
16lz
2021-01-24
需求是屏幕每旋转到一定角度时,通过做一些操作,比如给webview发送消息
具体实现:
onCreate 方法里
//屏幕方向监听 OrientationEventListener mOrientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { // Log.v(DEBUG_TAG,"Orientation changed to " + orientation); if (orientation > 350 || orientation < 10) { //0度 orientation = 0; Point outSize = new Point(); getWindowManager().getDefaultDisplay().getRealSize(outSize); int x = outSize.x; int y = outSize.y; System.out.println("x = " + x + ",y = " + y); System.out.println("xxxxxxxxx:竖屏 "); try { JSONObject obj = new JSONObject(); obj.put("width", x); obj.put("height", y); obj.put("orientation", "portrait"); JavascriptWXBridge.SrcChange(obj); } catch (JSONException e) { e.printStackTrace(); } } else if (orientation > 80 && orientation < 100) { //90度 orientation = 90; Point outSize = new Point(); getWindowManager().getDefaultDisplay().getRealSize(outSize); int x = outSize.x; int y = outSize.y; System.out.println("x = " + x + ",y = " + y); System.out.println("xxxxxxxxx:向右横屏 "); try { JSONObject obj = new JSONObject(); obj.put("width", x); obj.put("height", y); obj.put("orientation", "landscape"); JavascriptWXBridge.SrcChange(obj); } catch (JSONException e) { e.printStackTrace(); } } else if (orientation > 170 && orientation < 190) { //180度 orientation = 180; Point outSize = new Point(); getWindowManager().getDefaultDisplay().getRealSize(outSize); int x = outSize.x; int y = outSize.y; System.out.println("x = " + x + ",y = " + y); System.out.println("xxxxxxxxx:竖屏 "); try { JSONObject obj = new JSONObject(); obj.put("width", x); obj.put("height", y); obj.put("orientation", "portrait"); JavascriptWXBridge.SrcChange(obj); } catch (JSONException e) { e.printStackTrace(); } } else if (orientation > 260 && orientation < 280) { //270度 orientation = 270; Point outSize = new Point(); getWindowManager().getDefaultDisplay().getRealSize(outSize); int x = outSize.x; int y = outSize.y; System.out.println("x = " + x + ",y = " + y); System.out.println("xxxxxxxxx:向左横屏 "); try { JSONObject obj = new JSONObject(); obj.put("width", x); obj.put("height", y); obj.put("orientation", "landscape"); JavascriptWXBridge.SrcChange(obj); } catch (JSONException e) { e.printStackTrace(); } } else { return; } } };
更多相关文章
- Android(安卓)OpenGL ES(五):GLSurfaceView
- android sqlite 操作 说明 记录
- Android(安卓)ApiDemos示例解析(117):Views->Focus->3. Circular
- Android(安卓)sensor 实用篇
- 10.ScrollView
- Android(安卓)多点触控(放大、缩小、旋转、位移)
- android按屏幕大小动态确定控件位置及大小
- android根据ListView内部Item最大长度来设置ListView的宽度
- android 屏幕保持唤醒 不锁屏