[英]how to detect orientation of android device?
Is it possible simple to detect current orientation of android device, without programming a listener and handling the position matrix? 是否可以简单地检测Android设备的当前方向,而无需编程监听器和处理位置矩阵? 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.
但是,我不想听取轴测量或其他事件的事件。
您还可以使用:
getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
Use the getRotation
method: 使用
getRotation
方法:
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
, orSurface.ROTATION_270
.返回的值可能是
Surface.ROTATION_0
(无旋转),Surface.ROTATION_90
,Surface.ROTATION_180
或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 eitherSurface.ROTATION_90
orSurface.ROTATION_270
depending on the direction it was turned.例如,如果设备具有自然高的屏幕,并且用户将其侧面转向横向,则此处返回的值可以是
Surface.ROTATION_90
或Surface.ROTATION_270
具体取决于它的转向。 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
.例如,如果设备逆时针旋转90度,则补偿渲染将顺时针旋转90度,因此返回的值将为
Surface.ROTATION_90
。
Keep in mind that getRotation
was introduced from Android 2.2. 请记住,
getRotation
是从Android 2.2引入的。 Use getOrientation
if your target are older devices. 如果您的目标是旧设备,请使用
getOrientation
。
有一篇关于这个主题的好文章,这是一篇不得不读的文章: One Screen Turn Deserves Another
I think the safest and easiest way is to add a tag for some element of your activity in XML. 我认为最安全和最简单的方法是为XML中的某些活动元素添加标记。 For example, set the viewpager's tag to "portrait" in the portrait layout and set it to "landscape" in in landscape.
例如,在纵向布局中将viewpager的标记设置为“纵向”,并将其设置为横向“横向”。 Then in your oncreate check for that tag like so:
然后在你的oncreate检查那个标签,如下所示:
if(mViewpager.getTag().equals("portrait"))
// is in portrait
else
// is in 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
}
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: 在资源目录中,使用以下内容创建
constants.xml
文件:
<?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 (eg vertical / horizontal layout for a Recyclerview in a Fragment: 您的代码就这么简单(例如片段中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)
}
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.