Android 监听手机旋转角度
Posted 胡刚2021
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 监听手机旋转角度相关的知识,希望对你有一定的参考价值。
参考:
android手机旋转方向识别
gitee代码链接:
检测手机旋转角度
代码极其简单,而且不需要配置 androidManifest.xml。
package com.example.detectorientation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.OrientationEventListener;
public class MainActivity extends AppCompatActivity
private String TAG = "MainActivity";
private OrientationEventListener mOrientationListener;
private final void startOrientationChangeListener()
mOrientationListener = new OrientationEventListener(this)
@Override
public void onOrientationChanged(int rotation)
Log.e(TAG, " " + rotation);
// 当 rotation = -1 时,表示手机是 水平 放置的!!!
;
mOrientationListener.enable();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startOrientationChangeListener();
Called when the orientation of the device has changed. orientation parameter is in degrees, ranging from 0 to 359. orientation is 0 degrees when the device is oriented in its natural position, 90 degrees when its left side is at the top, 180 degrees when it is upside down, and 270 degrees when its right side is to the top. ORIENTATION_UNKNOWN is returned when the device is close to flat and the orientation cannot be determined.
Params:
orientation – The new orientation of the device.
See Also:
ORIENTATION_UNKNOWN
abstract public void onOrientationChanged(int orientation);
以上是关于Android 监听手机旋转角度的主要内容,如果未能解决你的问题,请参考以下文章