【问题标题】:SurfaceView rotation issue on Android 6Android 6 上的 SurfaceView 旋转问题
【发布时间】:2020-03-17 12:01:58
【问题描述】:

我有一个运行我的 OpenCV 应用程序的简单活动。该应用程序应检测来自相机的对象并在自定义 SurfaceView 上可视化它们。我想将设备方向锁定为纵向模式,并像标准相机应用一样以编程方式旋转我的小部件。

这是活动布局。它包括 OpenCV CameraView 和上面显示的片段小部件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.chessking.android.vision.ui.widget.ExtendedCameraView
        android:id="@+id/track_camera_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        opencv:camera_id="any"
        opencv:show_fps="true"/>

    <FrameLayout
        android:id="@+id/track_fragment_container"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true">

        <fragment
            android:id="@+id/track_board_preview"
            class="com.chessking.android.vision.ui.BoardWidgetFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>

    <Button
        android:id="@+id/track_action"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="16dp"
        android:text="@string/scan_action"/>
</RelativeLayout>

自定义SurfaceView 显示在片段布局中。是com.convekta.android.chessboard.ChessPanel

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/board_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/boardBackground"
    android:orientation="vertical">

    <include layout="@layout/widget_board_clock_both" />

    <com.convekta.android.chessboard.ChessPanel
        android:id="@+id/board_panel"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/board_background_transparent"
        app:chessPanelBackground="@color/board_background_transparent"
        app:chessPanelBorderColor="@color/board_border"
        app:chessPanelBorderWidth="1dp"/>

    <com.convekta.android.chessboard.NotationView
        android:id="@+id/nota"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:clipChildren="false"
        android:fadeScrollbars="false" />

</LinearLayout>

当我需要旋转小部件层时,我更改了board_layoutrotation 属性。

boardLayout.rotation = degrees.toFloat()

问题是旋转在 Android 9 上有效,但画布在 Android 上放错了位置。这是来自 Android 6 模拟器的屏幕截图,在纵向模式下,应用看起来很正常,在横向模式下,SurfaceView 会跳出布局。

【问题讨论】:

标签: android android-layout surfaceview


【解决方案1】:

AndroidManifest.xml&lt;activity&gt;标签中,添加android:screenOrientation="portrait"这一行

【讨论】:

  • 我已经锁定了。我需要锁定活动,而片段可以旋转。据我搜索,我需要以编程方式旋转片段。我已经这样做了,但它不适用于 Android 6 上的 SurfaceView ..
猜你喜欢
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多