【问题标题】:Activate camera inside a tab (android)在标签内激活相机(android)
【发布时间】:2011-07-14 19:41:43
【问题描述】:

我正在尝试开发具有 3 个选项卡的应用程序,以便第一个选项卡启用相机并捕获图像。到目前为止,我的相机有 2 个问题:

  1. 我希望相机锁定为纵向或横向模式。如果我将相机锁定为纵向,则图像似乎已旋转,并且我已使用 camera.parameters 尝试了几乎所有方法,但无法修复它。如果我将相机锁定在横向模式,那么问题在于选项卡菜单也将方向更改为横向。有没有办法只在该选项卡内更改方向并将选项卡菜单保持为纵向模式?

  2. 图像以某种方式被拉伸。我猜问题是相机使用全屏的高度和宽度,但我的应用程序试图将相机预览放在比屏幕小的框架布局内,因为选项卡菜单,所以图像被拉伸。有什么办法可以解决这个问题并显示带有标签菜单的实际相机预览?

我使用 1 个活动来创建 3 个选项卡并使用此布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_weight="1"/>
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>
  </LinearLayout>
</TabHost>

我为每个标签使用 1 个活动。特别是对于第一个选项卡(相机),我在以下布局中使用 preview.class:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="wrap_content">
<FrameLayout android:layout_weight="1" android:id="@+id/preview" android:layout_gravity="fill_vertical" android:fitsSystemWindows="true" android:layout_height="wrap_content" android:layout_width="wrap_content">
</FrameLayout>
<Button android:text="Click" android:id="@+id/buttonClick" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center"></Button>
</LinearLayout>

【问题讨论】:

    标签: android tabs camera orientation


    【解决方案1】:

    我使用 3 个选项卡完成此操作,其中第一个选项卡必须是纵向模式下的相机预览,且不失真。 好吧,这可以通过 3 个技巧实现 :)

    首先将您的子预览活动设置为纵向模式以强制标签处于纵向。 在 onCreate() 中使用

    this.getParent().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    或在清单中

    android:screenOrientation="portrait"
    

    然后要旋转屏幕,您不应该使用参数,因为它不起作用但是(仅从 2.2 开始)

    if (Build.VERSION.SDK_INT >= 8 ) camera.setDisplayOrientation(90);
    

    最后,为了避免拉伸,您可以将 FameLayout 全屏显示并使其进入选项卡下方(选项卡就像一个叠加层,但下方的摄像头是全屏且不失真)

    <TabWidget
    android:id="@android:id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"/>
    <FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@android:id/tabs"/>
    

    瞧!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      相关资源
      最近更新 更多