将surfaceView(带相机预览)移动到屏幕左侧 [英] Moving surfaceView (with camera preview) to the left of the screen

查看:38
本文介绍了将surfaceView(带相机预览)移动到屏幕左侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 Android 应用程序,该应用程序使用设备摄像头来拍摄照片和录制视频.此应用程序必须在 API 级别 >= 8 (Froyo) 上运行.

I'm currently developing an Android application that uses the device camera for both taking photos and recording videos. This application must work on API level >= 8 (Froyo).

到目前为止,我已经基于 Android 示例实现了相机代码.它可以在 <ADT folder>/sdk/samples/<android_version>/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.java 中找到 - 我正在关注android 11"示例在相机和android 8"之间切换.这意味着我的相机预览由包含在ViewGroup(CameraPreview 类)中的SurfaceView 保持反过来又作为主要活动中的内容视图附加.

Until now, I have implemented the camera code based on Android samples. It can be found in <ADT folder>/sdk/samples/<android_version>/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.java - I'm following the "android 11" example to switch between cameras and "android 8" for the rest. This means that my camera preview is held by a SurfaceView which is contained in a ViewGroup (CameraPreview class) which is in turn attached as the content view in the main activity.

问题是我不希望像 Android 那样显示相机预览.我的意思是:我希望预览适合屏幕尺寸之一并溢出另一个.Android 示例适合其中一个屏幕尺寸,并将另一个保持在屏幕边界内.

The problem is that I don't want the camera preview to be shown as Android does. I mean: I want the preview to fit one of the screen dimensions and overflow the other. Android samples fit one of the screen dimensions and keep the other inside the screen boundaries.

例如:让我们以屏幕尺寸为 480x800 的 Android 设备为例.我们选择 480x640 的预览尺寸.Android 会保持这个预览尺寸,因为它适合设备的屏幕:它会完全适合屏幕宽度,同时它会上下显示两个黑色矩形(信箱).就我而言,我将该预览放大 1.25,以便将其大小调整为 600x800(即,使预览和相机的最长边长为 800).然后我尝试通过将预览表面 (surfaceView) 稍微向左移动来使该预览居中.在这种情况下,最左侧的水平坐标将为 -60,最顶部为 0.

For example: let's take an Android device whose screen dimensions are 480x800. We select a preview size of 480x640. Android would keep this preview size as it fits within the device's screen: it would exactly fit the screen width, while it would show two black rectangles up and down (letterbox). In my case, I'd upscale that preview by 1.25, so as to resize it to 600x800 (that is, making the longest side of both the preview and the camera 800 long). Then I try to center this preview by moving that preview surface (surfaceView) slightly to the left. In this case, the leftmost horizontal coordinate would be -60 and topmost 0.

我的问题是我无法正确移动SurfaceView.它已正确调整大小,但我无法将其移动到左侧(屏幕外). 在 SO 上搜索,我找到了许多可能的解决方案,但没有一个对我有用.我确信这不应该那么困难,而且我一定是做错了什么.

My problem is that I'm not able to correctly move the SurfaceView. It is correctly resized but I cannot move it to the left (out of the screen). Searching on SO, I've found lots of possible solutions but none worked for me. I'm sure that this shouldn't be so difficult and I must be doing something wrong.

到目前为止,我已经尝试了一些解决方案,例如:

Until now, I've tried some solutions such as:

  • 按照 Android 示例,我将其调整大小代码 - onMeasure 函数 - 更改为:
  • Following the Android example , I changed it's resizing code - onMeasure function - to:
if (width * previewHeight < height * previewWidth) {

    final int scaledChildWidth = previewWidth * height / previewHeight;

    child.layout( (width - scaledChildWidth) / 2, 0, (width + scaledChildWidth) / 2, height);


} else {

    final int scaledChildHeight = previewHeight * width / previewWidth;

    child.layout(0, (height - scaledChildHeight) / 2, width, (height + scaledChildHeight) / 2);

}

这正确地将预览调整为我寻找的尺寸,但是,虽然最左边的坐标应该是 -60,但它继续从屏幕的左上角呈现.我只是在此处粘贴 onMeasure 函数中的更改,因为其余代码与我之前所说的 Android 示例文件夹中的代码相同.

This correctly resizes the preview to the dimensions I look for but, although leftmost coordinate is supposed to be -60, it continues to be rendered from the upper left corner of the screen. I'm just pasting here the change in onMeasure function as it the rest of the code is the same as the one in the Android sample folder I said before.

  • SurfaceView 的父容器更改为 ScrollView 并滚动预览.

  • Changing the parent container of the SurfaceView into a ScrollView and scrolling the preview.

ViewGroup/RelativeLayout/FrameLayout/LinearLayout/etc 中使用负边距或填充.在这种情况下,我可以移动一些界面元素(我添加到布局中的其他按钮),但不能移动 SurfaceView.

Using negative margins or paddings with ViewGroup/RelativeLayout/FrameLayout/LinearLayout/etc. In this case, I was able to move some interface elements (other button I added to the layout) but not the SurfaceView.

试图将 clipChildrenclipToPadding 设置为 false.

Trying to set clipChildren and clipToPadding to false.

尝试使用 centerInParent 选项.

创建大小与 SurfaceView(缩放预览大小)相同的新父布局并将其居中.它也可以正确调整大小,但不会在屏幕上居中.

Creating a new parent layout whose size is the same as that of the SurfaceView (scaled preview size) and centering it. It also resizes correctly but doesn't center on the screen.

使用 AbsoluteLayout(是的,我知道它已被弃用;))

Using AbsoluteLayout (yes, I know it's deprecated ;) )

我现在不记得我尝试过的其他解决方案,但我认为这些是主要的.

I can't remember now other solutions I tried, but I think these are the main ones.

似乎无法阻止元素从屏幕左上角开始.

当然,可以要求任何需要的更多详细信息,我会尽快回复.我真的被困在这一点上,我将非常感谢提供的任何帮助.感谢大家花时间阅读本文:)

Of course, any further details needed can be requested and I'll try to answer ASAP. I got really stuck at this point and I'd be very grateful for any help provided. Thanks to all for spending time on reading this :)

编辑 1

我正在 Android 2.3.6 中测试此应用程序.

I am testing this application in Android 2.3.6.

推荐答案

找了好久才回答,不过我花了很多时间才弄明白.

Answer after a long time, but I spent lot of time figuring this out.

某些 android 设备(见下文)上的SurfaceView 无法超出显示边界.如果是这样,它会移动并居中,如果它太大而不适合.如果选择的分辨率与您的显示尺寸不匹配,您必须添加填充或拉伸图像.这是事实.

The SurfaceView on some android devices (see below) just cannot extend beyond the display boundaries. If it does, it shifted and centered, if it's too big to fit. If the chosen resolution does not match your display size, you either have to add padding or stretch the image. That's a fact.

其次,请务必选择与预览尺寸具有相同纵横比的图片尺寸,否则您将无法在图片中获得与您在预览中看到的完全相同的视口.如果差异不大,这在您的情况下可能无关紧要,但在我的情况下确实如此.

Secondly, be sure to choose a picture size with the same aspect ratio as your preview size, or you will not get the exactly same viewport in the picture as you saw in preview. This might not matter in your case, if the difference is not big, but it does in mine.

注意一些设备:我在我的旧 HTC Desire S(使用 Android 2.3)上遇到过这种行为.它可以在搭载 Android 5.1 的三星 S5 上正常工作.如果有人使用与此相关的信息(Android 版本或仅某些型号......)来改进答案,我会很高兴.

Note on some devices: I experienced this behavior on my old HTC Desire S (with Android 2.3). It works as expected with Samsung S5 with Android 5.1. I will be glad if someone improves the answer with information what is this related to (android version, or just some models...).

这篇关于将surfaceView(带相机预览)移动到屏幕左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆