如何检测Android设备的方向?

发布于 2024-10-18 21:38:55 字数 95 浏览 4 评论 0原文

是否可以简单地检测 Android 设备的当前方向,而无需编写侦听器并处理位置矩阵? 在我的应用程序中,我现在只想知道当前方向 - 垂直或水平。但是我不想听轴测事件或其他事件。

Is it possible simple to detect current orientation of android device, without programming a listener and handling the position matrix?
In my app I want only to know current orientation - vertical or horizontal - at the moment. However I don't want to listen to events of axiometry or other events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

听风吹 2024-10-25 21:38:55

您还可以使用:

getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE

You can also use:

getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
累赘 2024-10-25 21:38:55

使用 getRotation 方法

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

:文档:

返回屏幕从“自然”方向的旋转。返回的值可能是 Surface.ROTATION_0(无旋转)、Surface.ROTATION_90Surface.ROTATION_180Surface.ROTATION_270< /代码>。例如,如果设备具有自然高的屏幕,并且用户将其侧转为横向,则此处返回的值可能是 Surface.ROTATION_90Surface .ROTATION_270 取决于转动的方向。角度是屏幕上绘制图形的旋转方向,与设备的物理旋转方向相反。例如,如果设备逆时针旋转 90 度,为了补偿渲染将顺时针旋转 90 度,因此此处的返回值将为 Surface.ROTATION_90


请记住,getRotation 是从 Android 2.2 引入的。如果您的目标是较旧的设备,请使用 getOrientation

Use the getRotation method:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

From the documentation:

Returns the rotation of the screen from its "natural" orientation. The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270. For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device. For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.

Keep in mind that getRotation was introduced from Android 2.2. Use getOrientation if your target are older devices.

眼眸里的那抹悲凉 2024-10-25 21:38:55

我认为最安全、最简单的方法是在 XML 中为活动的某些元素添加标签。例如,在纵向布局中将viewpager的标签设置为“portrait”,在横向布局中将其设置为“landscape”。然后在 oncreate 中检查该标签,如下所示:

if(mViewpager.getTag().equals("portrait"))
  // is in portrait
else
  // is in landscape

I think the safest and easiest way is to add a tag for some element of your activity in XML. For example, set the viewpager's tag to "portrait" in the portrait layout and set it to "landscape" in in landscape. Then in your oncreate check for that tag like so:

if(mViewpager.getTag().equals("portrait"))
  // is in portrait
else
  // is in landscape
对不⑦ 2024-10-25 21:38:55

您只需要知道画布的高度和宽度...您的表面...您的显示器...等。

也许您可以通过以下方式获得:

if (canvas.getHeight()>canvas.getWidth() ) {
//portrait
}
else
{
//landscape
}

You just need to know the height and the width of your canvas... your surface... your monitor... etc.

maybe you can get it with :

if (canvas.getHeight()>canvas.getWidth() ) {
//portrait
}
else
{
//landscape
}
尽揽少女心 2024-10-25 21:38:55

您可以从上面的代码中创建一个扩展函数:

fun AppCompatActivity.getRotation(): Int{
    val display = (baseContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
    val rotation = display.rotation
    when(rotation){
        Surface.ROTATION_90 -> return R.integer.LANDSCAPE
        Surface.ROTATION_270 -> return R.integer.LANDSCAPE
        Surface.ROTATION_180 -> return R.integer.PORTRAIT
        Surface.ROTATION_0 -> return R.integer.PORTRAIT
        else ->
            return R.integer.PORTRAIT
    }
}

在您的资源目录中创建一个包含以下内容的 constants.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="LANDSCAPE">0</integer>
    <integer name="PORTRAIT">1</integer>
</resources>

然后您的代码就像(例如,Recyclerview 的垂直/水平布局)一样简单一个片段:

val a  = activity as AppCompatActivity
        val orientation = a.getRotation()
        when (orientation){
            R.integer.PORTRAIT -> rv.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL,false)
            R.integer.LANDSCAPE -> rv.layoutManager = LinearLayoutManager(activity, RecyclerView.HORIZONTAL,false)
        }

You could make an extension function from the code above:

fun AppCompatActivity.getRotation(): Int{
    val display = (baseContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
    val rotation = display.rotation
    when(rotation){
        Surface.ROTATION_90 -> return R.integer.LANDSCAPE
        Surface.ROTATION_270 -> return R.integer.LANDSCAPE
        Surface.ROTATION_180 -> return R.integer.PORTRAIT
        Surface.ROTATION_0 -> return R.integer.PORTRAIT
        else ->
            return R.integer.PORTRAIT
    }
}

In your resource directory create a constants.xml file with the following contents:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="LANDSCAPE">0</integer>
    <integer name="PORTRAIT">1</integer>
</resources>

You code is then as easy as (e.g. vertical / horizontal layout for a Recyclerview in a Fragment:

val a  = activity as AppCompatActivity
        val orientation = a.getRotation()
        when (orientation){
            R.integer.PORTRAIT -> rv.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL,false)
            R.integer.LANDSCAPE -> rv.layoutManager = LinearLayoutManager(activity, RecyclerView.HORIZONTAL,false)
        }
没有你我更好 2024-10-25 21:38:55

这可能不相关,但如果您偶然需要知道这一点,因为您想将设备锁定到其当前方向,您可以在以下两行代码之间切换。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);

This may not be relevant, but if by chance you need to know this because you want to lock the device into its’ current orientation you can toggle between the following two lines of code.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文