Android App獲取屏幕旋轉(zhuǎn)角度的方法
本文實(shí)例為大家分享了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實(shí)現(xiàn)屏幕旋轉(zhuǎn)四個(gè)方向準(zhǔn)確監(jiān)聽
- Android如何監(jiān)聽屏幕旋轉(zhuǎn)
- Android屏幕旋轉(zhuǎn)之橫屏豎屏切換的實(shí)現(xiàn)
- Android6.0開發(fā)中屏幕旋轉(zhuǎn)原理與流程分析
- Android webview旋轉(zhuǎn)屏幕導(dǎo)致頁面重新加載問題解決辦法
- Android屏幕旋轉(zhuǎn) 處理Activity與AsyncTask的最佳解決方案
- 詳解Android中Runtime解決屏幕旋轉(zhuǎn)問題(推薦)
- Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
- Android開發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
- Android實(shí)現(xiàn)簡單旋轉(zhuǎn)動(dòng)畫
相關(guān)文章
android語音即時(shí)通訊之錄音、播放功能實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了android語音即時(shí)通訊之錄音、播放功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android 提交或者上傳數(shù)據(jù)時(shí)的dialog彈框動(dòng)畫效果
我們在使用支付寶支付的時(shí)候會(huì)看到類似這種彈框動(dòng)畫效果,下面通過實(shí)例代碼給大家分享android 提交或者上傳數(shù)據(jù)時(shí)的彈框動(dòng)畫效果,感興趣的的朋友參考下2017-07-07Android在多種設(shè)計(jì)下實(shí)現(xiàn)懶加載機(jī)制的方法
這篇文章主要介紹了Android在多種設(shè)計(jì)下實(shí)現(xiàn)懶加載機(jī)制的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Android 開發(fā)仿簡書登錄框可刪除內(nèi)容或顯示密碼框的內(nèi)容
本文通過實(shí)例代碼給大家分享android開發(fā)中模仿簡書登錄框可刪除內(nèi)容或顯示密碼框的內(nèi)容,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-12-12Android實(shí)現(xiàn)的仿淘寶購物車demo示例
這篇文章主要介紹了Android實(shí)現(xiàn)的仿淘寶購物車demo示例,結(jié)合實(shí)例形式分析了Android購物車的功能、布局及邏輯實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07Android消息通知欄的實(shí)現(xiàn)方法介紹
本篇文章是對Android消息通知欄的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android使用surfaceView自定義抽獎(jiǎng)大轉(zhuǎn)盤
這篇文章主要為大家詳細(xì)介紹了Android使用surfaceView自定義抽獎(jiǎng)大轉(zhuǎn)盤,熟練掌握SurfaceVie實(shí)現(xiàn)抽獎(jiǎng)大轉(zhuǎn)盤,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android自定義Dialog實(shí)現(xiàn)通用圓角對話框
這篇文章主要為大家詳細(xì)介紹了Android自定義Dialog實(shí)現(xiàn)通用圓角對話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11