android - 在选项卡内激活相机(android)

标签 android tabs camera orientation

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

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

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

我使用 1 个 Activity 来创建 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 个 Activity 。特别是对于第一个选项卡(相机),我在以下布局中使用了 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>

最佳答案

我用 3 个选项卡做到了,第一个选项卡必须是纵向模式下的相机预览,没有失真。 好吧,这可以通过 3 个技巧实现:)

首先将您的子预览 Activity 设置为纵向模式,以强制选项卡为纵向。 要么在 onCreate() 中使用

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

或在 list 中

android:screenOrientation="portrait"

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

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

最后,为了避免拉伸(stretch),您可以全屏使用 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"/>

瞧瞧!

关于android - 在选项卡内激活相机(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5295705/

相关文章:

tabs - 空格与制表符——最佳实践?

java - addListener 按钮在 android studio 中的 fragment 中不起作用

javascript - 我的选项卡 jquery ui 创建后回调

android - android eclipse 的相机源代码

java - 如何从mysql php获取数据到android

java - 我可以限制 recyclerview 中每个触摸事件的 onScrolled() 方法调用次数吗?

android fragment livedata删除观察者不起作用

android - 将 SimpleXmlConverter 与 Retrofit 2.0 一起使用时出现 IllegalArgumentException : Unable to create converter for java. util.List<ModelClass>

iOS - 创建多个时间延迟的实时相机预览 View

Swift 相机应用程序 - 启用横向模式右/左