栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 监听手机旋转角度

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685689.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号