【问题标题】:Android how to keep view orientation constant when the device is rotatedAndroid如何在设备旋转时保持视图方向不变
【发布时间】:2013-12-25 19:47:00
【问题描述】:

有几个堆栈溢出问题涉及将活动保持在恒定方向,建议设置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 坐标系)。

【问题讨论】:

  • 使用 2 个布局 xmls .. 一个用于 Portarait,一个用于横向 .. 为了使其高效,只需将工具栏用作不同的布局,其余的在片段中。这样你只需要带有工具栏的 xml 作为 2 个文件..
  • 只需在 manifest.xml 的特定活动中添加 android:screenOrientation="portrait" 即可。你会得到你想要的。 :)
  • @HYS 如果你有你的解决方案,它的 gud 。如果没有,您可以分享错误:)
  • @subhalaxminayak android:screenOrientation="portrait" 不是解决方案,因为它似乎将整个屏幕保持在纵向模式。我希望其他视图重新布局为横向,只有位图视图保持纵向。谢谢。
  • @HYS 你只想保持图片不变??

标签: android android-layout


【解决方案1】:

在清单 xml 中的 Activity 中使用具有纵向值的屏幕方向属性

喜欢

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

【讨论】:

  • 正常情况下会像你的截图一样,如果不是这样,我们会尝试其他想法
  • 你能解释一下你的答案吗??
  • 在您的清单中,您声明了包含此工具栏和图像的此活动,因此只需在此声明中添加此属性 android:screenOrientation="portrait" 即可修复屏幕方向。没事吧?
  • @Andrain 尝试然后发表评论。没有测试你怎么能说它不起作用。根据这里提出的问题,上面的答案是完美的..测试并发表评论:)
  • ngesh 是对的。我已经测试过了。更好的是在发布之前测试您的答案。 @subhalaxminayak
【解决方案2】:

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

  1. 在布局文件夹中放一个xml,表示纵向布局
  2. 创建一个文件夹,命名为layout-land
  3. 将表示横向布局的xml放入layout文件夹中

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

另一种方式

当方向改变时,Activity 将重新启动,因此对该 Activity 进行布局,您可以根据方向为您的 Activity 设置布局。

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

希望对你有所帮助。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      在您的清单中使用此代码

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

      【讨论】:

        【解决方案5】:

        嗯,首先,您不需要两个布局。 仅创建一个带有图像视图和布局的 xml,其属性 align parent bottom 设置为 true。

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

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

        这段代码在 onCreate--> 下面的代码是在从相机或画廊拍摄时旋转图像。您可以根据自己的需要进行修改。为了便于理解,我在下面放了 cmets。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多