Android App获取屏幕旋转角度的方法
作者:海月汐辰 发布时间:2021-12-26 10:42:19
标签:Android,App,旋转
本文实例为大家分享了Android App获取屏幕旋转角度的具体代码,供大家参考,具体内容如下
一、获取屏幕旋转角度的方法是:int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
二、测试代码
1、getRotation\app\src\main\java\com\example\getrotation\MainActivity.java
package com.example.getrotation;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
final String TAG="rotation";
TextView mshow_rotation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_update).setOnClickListener(this);
mshow_rotation=(TextView) findViewById(R.id.show_rotation);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_update) {
int angle = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
switch (angle) {
case Surface.ROTATION_0:
Log.d(TAG, "Rotation_0");
mshow_rotation.setText(Integer.toString(0)+"℃");
break;
case Surface.ROTATION_90:
Log.d(TAG, "ROTATION_90");
mshow_rotation.setText(Integer.toString(90)+"℃");
break;
case Surface.ROTATION_180:
Log.d(TAG, "ROTATION_180");
mshow_rotation.setText(Integer.toString(180)+"℃");
break;
case Surface.ROTATION_270:
Log.d(TAG, "ROTATION_270");
mshow_rotation.setText(Integer.toString(270)+"℃");
break;
default:
Log.d(TAG, "Default Rotation!");
break;
}
}
}
}
2、布局文件activity_main.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/show_rotation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Rotation value"
tools:layout_editor_absoluteX="145dp"
tools:layout_editor_absoluteY="119dp" />
</android.support.constraint.ConstraintLayout>
三、执行效果
来源:https://blog.csdn.net/qq_37858386/article/details/103817938


猜你喜欢
- //==============================================// &n
- 效果图,每隔1秒,变换一下时间 xml: <RelativeLayout xmlns:android="http
- 本文实例为大家分享了Android实现记住账号密码的具体代码,供大家参考,具体内容如下布局一个复选框<CheckBox
- 分栏是报刊、书籍、杂志常用的排版样式,它不仅能方便阅读,同时也能增加页面的美观度。本文将介绍如何在Java应用程序中给Word文档添加多个栏
- Java8对集合提供了一种流式计算的方式,这种风格将要处理的元素集合看 作一种流, 流在管道中传输, 并且可以在管道的节点上进行处理, 比如
- SpringBoot启动类静态资源路径SpringBoot核心配置类SpringBoot核心JAR包--》spring-boot-autoc
- APP启动速度非常重要,APP启动速度慢,可能会造成用户体验不良好,尤其是在最近用Android studio之后,如果长时间不打开app,
- 我们已经有文章向你描述如何使用<include />标签来重用和共享你的布局代码。这篇文章将向你阐述<merge />
- 目录1.跳转控制语句:区分continue和break; * continue:用于循环中,基于条件控制,跳过某次循环体内容的执行
- 本文实例为大家分享了Android RecyclerView使用的具体代码,供大家参考,具体内容如下package com.itheima7
- 关于RecyclerViewRecyclerView在Android 5.0以来被引入,以前经常使用的ListView 继承的是AbsLis
- 一、前言在日常工作中,如果涉及到与第三方进行接口对接,有的会使用WebService的方式,这篇文章主要讲解在.NET Framework中
- Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Andro
- 事发地原默认的Feign是使用URLConnector进行通信的,当换为okhttp时,直接引入包及配置以下内容根本不生效,还是走原生的。f
- 如果运行react-native android项目出现如下错误:解决办法如下:一、执行adb devices,判断adb有没有断,二、如果
- 前言 在android开发中,很多的app都有使用侧滑菜单,有的是自定义控件来实现侧滑菜单,但是android给我们提供了DrawerLa
- Word是我们日常生活、学习和工作中必不可少的文档处理工具。精致美观的文档能给人带来阅读时视觉上的美感。在本篇文章中,将介绍如何使用组件Fr
- Android自定义View实现等级滑动条的实例实现效果图:思路: 首先绘制直线,然后等分直线绘制点; 绘制点的时候把X值存到集
- springboot 异常与重定向在spring中,有两个重定向类型:301,永久性跳转302,暂时性跳转默认调用302。1.下面先通过一个
- 第一个方法是计算出阶乘然后计算字符串的0的个数。import java.util.Scanner;import java.math.BigD