Gahing's blog Gahing's blog
首页
知识体系
  • 前端基础
  • 应用框架
  • 工程能力
  • 应用基础
  • 专业领域
  • 业务场景
  • 前端晋升 (opens new window)
  • Git
  • 网络基础
  • 算法
  • 数据结构
  • 编程范式
  • 编解码
  • Linux
  • AIGC
  • 其他领域

    • 客户端
    • 服务端
    • 产品设计
软素质
  • 面试经验
  • 人生总结
  • 个人简历
  • 知识卡片
  • 灵感记录
  • 实用技巧
  • 知识科普
  • 友情链接
  • 美食推荐 (opens new window)
  • 收藏夹

    • 优质前端信息源 (opens new window)
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Gahing / francecil

To be best
首页
知识体系
  • 前端基础
  • 应用框架
  • 工程能力
  • 应用基础
  • 专业领域
  • 业务场景
  • 前端晋升 (opens new window)
  • Git
  • 网络基础
  • 算法
  • 数据结构
  • 编程范式
  • 编解码
  • Linux
  • AIGC
  • 其他领域

    • 客户端
    • 服务端
    • 产品设计
软素质
  • 面试经验
  • 人生总结
  • 个人简历
  • 知识卡片
  • 灵感记录
  • 实用技巧
  • 知识科普
  • 友情链接
  • 美食推荐 (opens new window)
  • 收藏夹

    • 优质前端信息源 (opens new window)
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Android

    • 动态化技术

    • 应用开发

      • Android开发技巧
      • Android模拟触控解决方案
      • Android监听屏幕旋转
        • 需求
        • 方案
          • 方法一【结论不可行】
          • 方案二 利用重力传感器
      • adb控制avd
    • 系统原理

    • 语言基础

    • 逆向技术

  • iOS

  • 客户端
  • Android
  • 应用开发
gahing
2016/10/18
目录

Android监听屏幕旋转

# 需求

监听屏幕旋转,不重建Activity

大前提:Activity 已经限定屏幕竖屏

android:screenOrientation="portrait"
1

# 方案

# 方法一【结论不可行】

要想监听屏幕旋转,需要:

1.权限声明

<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>

2.声明activity要捕获的事件类型

 android:configChanges="orientation"

keyboardHidden表示键盘辅助功能隐藏,如果你的开发API等级等于或高于13,还需要设置screenSize,因为screenSize会在屏幕旋转时改变;

一般这样设

android:configChanges="keyboardHidden|orientation|screenSize"

优点:我们可以随时监听屏幕旋转变化,并对应做出相应的操作;

缺点:它只能一次旋转90度,如果一下子旋转180度,onConfigurationChanged函数不会被调用。

3.重写Activity中的onConfigurationChanged方法

@Override
public void onConfigurationChanged(Configuration newConfig) {
// 当新设置中,屏幕布局模式为横排时
	if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
	{
	//TODO 某些操作 
	}
	super.onConfigurationChanged(newConfig);
}

前提:未开启方向锁定 or 未设置 android:screenOrientation 方向固定or忽视传感器

与大前提不符

# 方案二 利用重力传感器

可拓展性好

package com.ws.tryplay.util;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.OrientationEventListener;

/**
 * Created by zhengjx on 2016/12/14.
*/

public class ScreenOrientationUtil {
   private OrientationEventListener mOrientationListener;
   private boolean mScreenPortrait = true;
   private boolean mCurrentOrient = false;
   private static final String TAG = "ScreenOrientationUtil";
   private OrientationEvent event;
   private Context mContext;
   public interface OrientationEvent{
       void orientationChanged(int orientation);
   }
   public ScreenOrientationUtil(OrientationEvent event, Context mContext) {
       this.event = event;
       this.mContext = mContext;
   }
   public void startOrientationChangeListener() {
       mOrientationListener = new OrientationEventListener(this.mContext) {
           @Override
           public void onOrientationChanged(int rotation) {
               if (((rotation >= 0) && (rotation <= 45)) || (rotation >= 315)||(rotation>=135)&&(rotation<=225))) {//portrait
                   mCurrentOrient = true;
                   if(mCurrentOrient!=mScreenPortrait)
                   {
                       mScreenPortrait = mCurrentOrient;
                       event.orientationChanged(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                       Log.d(TAG, "Screen orientation changed from Landscape to Portrait!");
                   }
               }
               else if (((rotation > 45) && (rotation < 135))||((rotation>225)&&(rotation<315))) {//landscape
                   mCurrentOrient = false;
                   if(mCurrentOrient!=mScreenPortrait)
                   {
                       mScreenPortrait = mCurrentOrient;
                       event.orientationChanged(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                       Log.d(TAG, "Screen orientation changed from Portrait to Landscape!");
                   }
               }
           }
       };
       mOrientationListener.enable();
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

Activity中去注册监听

private ScreenOrientationUtil.OrientationEvent orientationEvent=new ScreenOrientationUtil.OrientationEvent() {
    @Override
    public void orientationChanged(int orientation) {
        Log.d(TAG, "orientationChanged() called with: orientation = [" + orientation + "]");
    }
};

new ScreenOrientationUtil(orientationEvent,GameActivity.this).startOrientationChangeListener();
1
2
3
4
5
6
7
8
编辑 (opens new window)
上次更新: 2024/09/01, 23:56:56
Android模拟触控解决方案
adb控制avd

← Android模拟触控解决方案 adb控制avd→

最近更新
01
浅谈代码质量与量化指标
08-27
02
快速理解 JS 装饰器
08-26
03
Vue 项目中的 data-v-xxx 是怎么生成的
09-19
更多文章>
Theme by Vdoing | Copyright © 2016-2024 Gahing | 闽ICP备19024221号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式