Android App获取屏幕旋转角度的方法

 更新时间:2022年07月20日 12:39:29   作者:海月汐辰  
这篇文章主要为大家详细介绍了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>

三、执行效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • android语音即时通讯之录音、播放功能实现代码

    android语音即时通讯之录音、播放功能实现代码

    这篇文章主要为大家详细介绍了android语音即时通讯之录音、播放功能的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android组件实现列表选择框功能

    Android组件实现列表选择框功能

    android提供的列表选择框(Spinner)相当于web端用户注册时的选择下拉框,比如注册候选择省份城市等。这篇文章主要介绍了Android组件实现列表选择框功能,需要的朋友可以参考下
    2017-02-02
  • Android 提交或者上传数据时的dialog弹框动画效果

    Android 提交或者上传数据时的dialog弹框动画效果

    我们在使用支付宝支付的时候会看到类似这种弹框动画效果,下面通过实例代码给大家分享android 提交或者上传数据时的弹框动画效果,感兴趣的的朋友参考下
    2017-07-07
  • Android在多种设计下实现懒加载机制的方法

    Android在多种设计下实现懒加载机制的方法

    这篇文章主要介绍了Android在多种设计下实现懒加载机制的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • Android 开发仿简书登录框可删除内容或显示密码框的内容

    Android 开发仿简书登录框可删除内容或显示密码框的内容

    本文通过实例代码给大家分享android开发中模仿简书登录框可删除内容或显示密码框的内容,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2016-12-12
  • Android实现的仿淘宝购物车demo示例

    Android实现的仿淘宝购物车demo示例

    这篇文章主要介绍了Android实现的仿淘宝购物车demo示例,结合实例形式分析了Android购物车的功能、布局及逻辑实现技巧,需要的朋友可以参考下
    2016-07-07
  • Android 仿微信数字键盘

    Android 仿微信数字键盘

    大部分的金融App会对默认的数字键盘进行处理,以实现自定义的数字安全键盘。基于此,本文对对微信数字键盘样式进行了仿写,实现了一套自定义的数字安全键盘(支持随机数字分布)。
    2021-05-05
  • Android消息通知栏的实现方法介绍

    Android消息通知栏的实现方法介绍

    本篇文章是对Android消息通知栏的实现方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • Android使用surfaceView自定义抽奖大转盘

    Android使用surfaceView自定义抽奖大转盘

    这篇文章主要为大家详细介绍了Android使用surfaceView自定义抽奖大转盘,熟练掌握SurfaceVie实现抽奖大转盘,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Android自定义Dialog实现通用圆角对话框

    Android自定义Dialog实现通用圆角对话框

    这篇文章主要为大家详细介绍了Android自定义Dialog实现通用圆角对话框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11

最新评论