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>

三、执行效果

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值