Android App獲取屏幕旋轉(zhuǎn)角度的方法
本文實例為大家分享了Android App獲取屏幕旋轉(zhuǎn)角度的具體代碼,供大家參考,具體內(nèi)容如下
一、獲取屏幕旋轉(zhuǎn)角度的方法是: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>
三、執(zhí)行效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)屏幕旋轉(zhuǎn)四個方向準(zhǔn)確監(jiān)聽
- Android如何監(jiān)聽屏幕旋轉(zhuǎn)
- Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實現(xiàn)
- Android6.0開發(fā)中屏幕旋轉(zhuǎn)原理與流程分析
- Android webview旋轉(zhuǎn)屏幕導(dǎo)致頁面重新加載問題解決辦法
- Android屏幕旋轉(zhuǎn) 處理Activity與AsyncTask的最佳解決方案
- 詳解Android中Runtime解決屏幕旋轉(zhuǎn)問題(推薦)
- Android實現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
- Android開發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
- Android實現(xiàn)簡單旋轉(zhuǎn)動畫
相關(guān)文章
Android開發(fā)實現(xiàn)生成excel的方法詳解
這篇文章主要介紹了Android開發(fā)實現(xiàn)生成excel的方法,結(jié)合實例形式詳細(xì)分析了Android生成Excel的具體步驟與存儲、導(dǎo)入、添加等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
android之計時器(Chronometer)的使用以及常用的方法
在Android的SDK中,為我們提供了一個計時器,這個計時器稱為Chronometer,我們可以成它為Android的一個組件,同時它也具備自己獨有的方法2013-01-01
Android使用DrawerLayout實現(xiàn)仿QQ雙向側(cè)滑菜單
這篇文章主要介紹了Android使用DrawerLayout實現(xiàn)仿QQ雙向側(cè)滑菜單的方法和詳細(xì)代碼,有需要的小伙伴可以認(rèn)真參考下。2016-01-01
Android App中使用ViewPager實現(xiàn)滑動分頁的要點解析
這篇文章主要介紹了Android App中使用ViewPager實現(xiàn)滑動分頁的要點解析,還附帶了一個禁止ViewPager左右滑動的方法,需要的朋友可以參考下2016-06-06
Android筆記之:App應(yīng)用之啟動界面SplashActivity的使用
當(dāng)前比較成熟一點的應(yīng)用基本上都會在進(jìn)入應(yīng)用之顯示一個啟動界面.這個啟動界面或簡單,或復(fù)雜,或簡陋,或華麗,用意不同,風(fēng)格也不同2013-04-04
Android中TextureView與SurfaceView用法區(qū)別總結(jié)
TextureView和SurfaceView都是繼承自View類的,TextureView在Andriod4.0之后才引入的,SurfaceView不能加上動畫、平移、縮放,TextureView可以但有1-3幀的延遲2018-04-04

