Android 开发 - 获取当前设备屏幕的旋转角度

获取当前设备屏幕的旋转角度

1、基本介绍
java 复制代码
@Surface.Rotation
public int getRotation() {
    synchronized (mLock) {
        updateDisplayInfoLocked();
        return getLocalRotation();
    }
}
  • 返回一个 int 常量
返回值 常量 说明
0 Surface.ROTATION_0 未旋转 0°,竖屏,顶部朝上
1 Surface.ROTATION_90 顺时针旋转 90°,左横屏,顶部朝左
2 Surface.ROTATION_180 旋转 180°,倒置,顶部朝下
3 Surface.ROTATION_270 顺时针旋转 270°,右横屏,顶部朝右
  • 注:这里的顺时针是相对于设备的自然方向而言的,而不是相对于你(用户)的视觉方向
2、演示
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F5F5F5"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:lineSpacingExtra="12dp"
        android:textSize="24sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="\n点击屏幕刷新"
        android:textColor="#999"
        android:textSize="14sp" />
</LinearLayout>
java 复制代码
public class RotationTestActivity extends AppCompatActivity {

    private TextView tvInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_rotation_test);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.root), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        tvInfo = findViewById(R.id.tv_info);
        findViewById(R.id.root).setOnClickListener(v -> showRotation());

        showRotation();
    }

    private void showRotation() {
        int rotation = getWindowManager().getDefaultDisplay().getRotation();

        String[] map = {
                "0 → 竖屏(顶部朝上)",
                "1 → 左横屏(顶部朝左)",
                "2 → 倒置(顶部朝下)",
                "3 → 右横屏(顶部朝右)"
        };

        tvInfo.setText("getRotation() = " + rotation + "\n" + map[rotation]);
    }
}
相关推荐
野生技术架构师10 小时前
1000+ Java面试题知识图谱:从基础语法到分布式架构的完整拓扑
java·面试
步行cgn10 小时前
Spring 底层如何创建对象:反射机制与实例化策略
java·后端·spring
2501_9159184110 小时前
怎么把 Python 写的 Flet 应用打包成 iOS App 并上架 App Store?
android·ios·小程序·https·uni-app·iphone·webview
何以解忧,唯有..10 小时前
Redis 过期事件监听:原理与实战
java
风中的小熊生气11 小时前
Spring Boot 面试知识(二):分层、IoC、异常、配置与 Redis
java·springboot
泡茶喝茶写代码11 小时前
量化数据开发实战系列(第 25 篇):基金基础接口实战:基金列表、ETF-LOF 分类、基金概况、净值数据
java·数据库·人工智能·python·mysql
shehuiyuelaiyuehao11 小时前
算法46,分治快排,排序数组
java·算法·排序算法·排序
野生技术架构师11 小时前
1200 道 JAVA 面试题(各大企业常见面试题及答案)
java·开发语言
careathers11 小时前
【数据结构】栈
java·数据结构
邪修king12 小时前
Re:Linux系统篇(二十六):文件系统(二):Ext 文件系统底层详解:从 inode、块组到软硬链接,结合 Windows 讲透文件管理本质
android·java·linux