Android 监听手机旋转角度

Android 监听手机旋转角度,第1张

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);

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5686237.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存