【问题标题】:ImageView/ImageButton overlay on camera preview not displayed未显示相机预览上的 ImageView/ImageButton 叠加层
【发布时间】:2017-06-23 08:32:13
【问题描述】:

此链接中的一个非常标准的叠加层tutorial 用于开发带有叠加层的相机预览。但是,当我将 <Button/> 更改为 <ImageView/> 或 <ImageButton/> 时,图像不会显示。我得到的只是使用<ImageView/> 和使用<ImageButton/> 时的相机预览,图像没有显示,但是在应该显示图像的地方有小白线。图像存在于mipmap-xxhdpi。这些按钮将用于启动另一个活动或在用户点击时拍照。

我查看了有关 StackOverFlow 的问题,但回答 here 对我不起作用。

其他相关详情

minSdkVersion 是 16,compileSdkVersion 是 25。当我在 API-21 或以上的模拟器中运行应用程序时,我得到的是黑屏。

Q1:如何在相机预览上叠加<ImageView/>或<ImageButton/>?

Q2:黑屏是否与相机 API(旧)被弃用有关,我必须构建单独的类以考虑不同的 API?

喜欢下面的代码

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    //do something
} else {
    //do something else
}

【问题讨论】:

标签: android camera imageview overlay imagebutton


【解决方案1】:

根据您的第一个问题,我认为问题在于您使用的表面视图的高度大于 imageView 的高度并且等于 imageButton 因此在 ImageView 的情况下没有任何显示,在 ImageButton 的情况下显示线条(提示:想想 3d)here 是 android 材料设计高程和阴影的指南。尝试增加 ImageView 或 ImageButton 的高程。

现在对于您的第二个问题,它不应该是因为旧 API 尝试this 答案。

【讨论】:

  • 我使用bringToFront() 和ViewCompat.setTranslationZ(view, translationZ) 尝试了您的建议。但是覆盖布局没有出现在相机中。我仍在阅读有关在打开相机时出现黑屏的各种资料。在 API 16 到 API 19 上打开应用程序中的相机时,它可以正常工作
【解决方案2】:

所以我的问题在模拟器中没有得到解决。但是,我能够接触到运行 API15、19、21、21、24 的一系列设备 (5)。这是对我有用的代码。只有在我将 minSdkVersion 更改为 15 后才使用 API15。

使用here 给出的步骤构建您的自定义相机。何时编辑 XML。使用此代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"/>

<ImageView
    android:id="@+id/button_capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/your_file_name"/>
</FrameLayout>

至于黑屏,必须在API21及以上的设备设置(Settings-&gt;App-&gt;Select your app-&gt;Permisions-&gt;Camera)内给予应用权限。

编辑:通过invalidating cache and restarting 得到相同的代码在模拟器中工作,然后使用clean project。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 2012-10-06
    相关资源
    最近更新 更多