的Andr​​oid如何保持视图方向不变,当设备旋转 [英] Android how to keep view orientation constant when the device is rotated

查看:218
本文介绍了的Andr​​oid如何保持视图方向不变,当设备旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个堆栈溢出的问题,与保持活性,以恒定的方向处理,提示或者设置<一个href=\"http://stackoverflow.com/questions/15815353/force-orientation-to-view\">android:screenOrientation (或<一个href=\"http://stackoverflow.com/questions/4277596/android-setting-activity-orientation-by-$c$c\">this)或处理配置更改手动的。

There are several stack overflow questions that deal with keeping the activity in a constant orientation, suggesting either to set android:screenOrientation (or this) or to deal with the configuration changes manually.

然而,如果希望保持自动布局对大多数的意见的好处,但保持一种观点认为在一个恒定的方向,这些答案都没有解决问题。比如我有内容的一个主视图(位图)和可选的几个工具栏。当设备从纵向旋转为横向,我希望工具栏自动重新排版。另一方面,主要内容视图具有一个位图,这应该与装置旋转。下面是一个简单的code我在使用的onDraw:

However, these answers do not solve the problem if one wants to keep the benefits of automatic layout for most of the views, but to keep one view in a constant orientation. For example I have one main view for the content (a bitmap) and optionally several "toolbars". When the device is rotated from portrait to landscape, I want the toolbars to re-layout automatically. On the other hand, the main content view has a bitmap, and this should rotate with the device. Here is a simple code I use in my onDraw:

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Bitmap bm = ... // code to get a bitmap
        if (bm != null) {
            canvas.drawBitmap(bm,0,0,mPaint);
        }
    }

这显然并不如预期,因为当设备取向的变化,该视图是重布局及其坐标系中90°该取向变化相对于物理设备旋转工作。单程以补偿将是使用一个矩阵对象如果设备方向已经改变以90度的旋转来绘制位图。但是,这时也输入坐标将必须以保持视图输入点和对应于位图中的点被改变。

This obviously does not work as intended because when the device orientation changes, the view is relayout and its coordinate system rotates in this orientation change by 90 degrees with respect to the physical device. One way to compensate for that would be to use a Matrix-object to draw the bitmap with 90 degrees rotation if the device orientation has changed. However, then also the input coordinates would have to be changed in order to keep the view input points and the point on the bitmap in correspondence.

因此​​,本长篇解释后,我的问题是:我可以保持视图,以便其坐标系统相对于设备不旋转时,方向的变化

Therefore my question after this long explanation is: can I keep the view so that its coordinate system with respect to the device is not rotated when the orientation changes.

下面是一个图片的前presses我想和我目前得到(XY坐标系的观点也说明了)的东西。

Here is a picture that expresses what I want and what I currently get (xy coordinate system for the view also illustrated).

推荐答案

在清单XML您的活动内使用带有纵向价值屏幕方向attribut

Inside your activity in manifest xml use the screen orientation attribut with portrait value

如同

 <activity
        android:name=".YourActivityName"
        android:screenOrientation="portrait" >
    </activity>

这篇关于的Andr​​oid如何保持视图方向不变,当设备旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆