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开发实现生成excel的方法详解

    Android开发实现生成excel的方法详解

    这篇文章主要介绍了Android开发实现生成excel的方法,结合实例形式详细分析了Android生成Excel的具体步骤与存储、导入、添加等相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • android之计时器(Chronometer)的使用以及常用的方法

    android之计时器(Chronometer)的使用以及常用的方法

    在Android的SDK中,为我们提供了一个计时器,这个计时器称为Chronometer,我们可以成它为Android的一个组件,同时它也具备自己独有的方法
    2013-01-01
  • Android实现秒表功能

    Android实现秒表功能

    这篇文章主要为大家详细介绍了Android实现简易秒表功能,具备启停功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • Android使用DrawerLayout实现仿QQ双向侧滑菜单

    Android使用DrawerLayout实现仿QQ双向侧滑菜单

    这篇文章主要介绍了Android使用DrawerLayout实现仿QQ双向侧滑菜单的方法和详细代码,有需要的小伙伴可以认真参考下。
    2016-01-01
  • Android UI控件Switch的使用方法

    Android UI控件Switch的使用方法

    这篇文章主要为大家详细介绍了Android UI控件Switch的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • Android编程之MD5加密算法实例分析

    Android编程之MD5加密算法实例分析

    这篇文章主要介绍了Android编程之MD5加密算法,结合实例形式较为详细的分析了Android使用MD5加密的具体实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • Android App中使用ViewPager实现滑动分页的要点解析

    Android App中使用ViewPager实现滑动分页的要点解析

    这篇文章主要介绍了Android App中使用ViewPager实现滑动分页的要点解析,还附带了一个禁止ViewPager左右滑动的方法,需要的朋友可以参考下
    2016-06-06
  • Android笔记之:App应用之启动界面SplashActivity的使用

    Android笔记之:App应用之启动界面SplashActivity的使用

    当前比较成熟一点的应用基本上都会在进入应用之显示一个启动界面.这个启动界面或简单,或复杂,或简陋,或华丽,用意不同,风格也不同
    2013-04-04
  • Android实现加载圈

    Android实现加载圈

    这篇文章主要为大家详细介绍了Android实现加载圈,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Android中TextureView与SurfaceView用法区别总结

    Android中TextureView与SurfaceView用法区别总结

    TextureView和SurfaceView都是继承自View类的,TextureView在Andriod4.0之后才引入的,SurfaceView不能加上动画、平移、缩放,TextureView可以但有1-3帧的延迟
    2018-04-04

最新评论