Android如何在旋转设备时保持视图方向不变

时间:2013-12-25 10:17:31

标签: android android-layout

有几个堆栈溢出问题涉及将活动保持在一个恒定的方向,建议设置android:screenOrientation(或this)或deal with the configuration changes manually

然而,如果想要保持大多数视图的自动布局的好处,但是为了使一个视图保持恒定方向,这些答案并不能解决问题。例如,我有一个主要视图的内容(位图)和可选的几个“工具栏”。当设备从纵向旋转到横向时,我希望工具栏自动重新布局。另一方面,主内容视图有一个位图,这应该随设备一起旋转。这是我在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度。补偿这种情况的一种方法是,如果设备方向已更改,则使用Matrix对象以90度旋转绘制位图。然而,还必须改变输入坐标以便保持视图输入点和位图上的点对应。

因此,经过这么长时间的解释,我的问题是:我可以保持视图,以便在方向改变时不会旋转其相对于设备的坐标系。

这是一张图片,表达了我想要的和我目前得到的东西(视图的xy坐标系也如图所示)。

enter image description here

6 个答案:

答案 0 :(得分:2)

在manifest xml中的活动内部,使用带纵向值的屏幕方向属性

喜欢

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

答案 1 :(得分:1)

我不确定,但您应该尝试关注

  1. 将一个xml放在布局文件夹中,表示纵向布局
  2. 创建一个名为layout-land
  3. 的文件夹
  4. 将xml表示与layout文件夹
  5. 中的名称相同的横向布局

    当您将屏幕旋转到横向时,Android会自动从land-layout获取该布局。

    另一种方式

    当方向发生变化时,活动将重新启动,以便为该活动制作布局,您可以根据方向将布局设置为您的活动。

    if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
    {
       setContentView(R.layout.landscape_layout);
    }
    else
    {
       setContentView(R.layout.portrait_layout);
    }
    

    希望它会对你有所帮助。

答案 2 :(得分:0)

您可以检查方向并在更改方向后重新绘制视图

if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
{
//...
}
else
{
//...
}

答案 3 :(得分:0)

  

在您的清单中使用此代码

<uses-permission android:name="android.permission.SET_ORIENTATION"/>
<activity
        android:name="com.package.MyActivity"
        android:configChanges="keyboardHidden|orientation"
        android:screenOrientation="portrait" >
    </activity>

答案 4 :(得分:0)

首先,您不需要两个布局。 仅创建一个带有图像视图的xml和一个将属性对齐父底部设置为true的布局。

现在,根据您提供的图像,您希望在设备处于横向状态时逆时针旋转图像。为此,您可以使用我的图像旋转代码: -

您始终可以使用Matrix检查图像的旋转并相应地旋转它。

此代码进入onCreate - &gt; 下面的代码是从相机或图库中拍摄时旋转图像。您可以根据自己的需要进行修改。我在下面写了评论以简化理解。

        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inPurgeable = true;

            Bitmap cameraBitmap = BitmapFactory.decodeFile(filePath);//You convert your image view image here in a bitmap
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);


            ExifInterface exif = new ExifInterface(filePath);
            float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
            System.out.println(rotation);

            float rotationInDegrees = exifToDegrees(rotation);


            System.out.println(rotationInDegrees);
            //These three lines below set the rotation. 
            //Put an if condition here to check device rotation and set the rotation of image view as per the device rotation.
//The line below will help you get device rotation.    
//getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE
                Matrix matrix = new Matrix();
                matrix.postRotate(rotationInDegrees);//Here you set the rotation value in which you want your image view to be rotated
            //If ends here

            Bitmap scaledBitmap = Bitmap.createBitmap(cameraBitmap);
            Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
            FileOutputStream fos=new FileOutputStream(filePath);
            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
                        //this rotatedbitmap you can set in your image view

//onCreate Code Ends here.

//This function below is used to get rotation:-

        private static float exifToDegrees(float exifOrientation) {        
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } 
        else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {  return 180; } 
        else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  return 270; }            
        return 0;    
     }

答案 5 :(得分:-1)

您应该在宣布活动的清单中将screenOrientation设置为portrait: -

    <activity
        android:name="com.package.MyActivity"
        android:configChanges="keyboardHidden|orientation"
        android:screenOrientation="portrait" >
    </activity>

但是这段代码也把你的工具栏放在了画像中。

相关问题