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]);
    }
}
相关推荐
狼爷6 小时前
Rust/Go/Java/Python/PHP 大比拼:负载下后端框架到底差多少?
java·后端·编程语言
念何架构之路6 小时前
zap扩展生态与总结
java·前端·数据库
第七页独白7 小时前
汽车零件厂如何通过 QMS 真正落地 IATF 16949——QMS软件系统:品质检验-内审稽核-8d客诉管理:全星质量管理软件系统
java·前端·数据库
传奇开心果编程7 小时前
【Compose Multiplatform 跨端开发学与练】第7课 平台适配与互操作
android·windows·学习·ui·ios·kotlin·composer
mmsx7 小时前
Android 地图数据链路:从 GDAL、KML 到多引擎适配
android·kotlin
QCoding7 小时前
Spring AI Alibaba Graph实战:从ReAct Agent到Workflow,企业AI复杂流程该如何编排?
java·人工智能
用户094248568037 小时前
第25章:Java虚拟线程(Project Loom)实战与调度协作
java·jvm
2601_968900777 小时前
大模型版本回归评估实战:从能力指标到额度口径的完整链路
android·数据挖掘·回归
砚底藏山河7 小时前
量化实战:截面因子有效性检验(IC 分析与分层回测)
java·python·金融·maven
wuminyu7 小时前
ForkJoinPool内部WorkQueue的Lock-Free数组操作以及并发任务窃取原理剖析
java·linux·c语言·jvm·c++