覆盖图像按钮不可见(Android)
我想在我的相机应用程序中的相机预览顶部覆盖一个图像按钮,这样相机预览可以占据整个屏幕,用户仍然可以拍照 我的活动\u camera.xml是:覆盖图像按钮不可见(Android),android,layout,android-camera,Android,Layout,Android Camera,我想在我的相机应用程序中的相机预览顶部覆盖一个图像按钮,这样相机预览可以占据整个屏幕,用户仍然可以拍照 我的活动\u camera.xml是: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/camera_photo"
android:id="@+id/take_photoimageButton"
android:visibility="visible"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
</LinearLayout>
是否有一种方法可以编辑布局,使按钮在相机预览时可见
编辑-
这是我的预习课:
class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context, Camera inCamera) {
super(context);
camera = inCamera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void OLDsurfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
// camera = Camera.open();
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(holder);
// camera.setPreviewCallback(new PreviewCallback() {
//
// public void onPreviewFrame(byte[] data, Camera arg1) {
// FileOutputStream outStream = null;
// try {
// outStream = new FileOutputStream(String.format(
// "/sdcard/%d.jpg", System.currentTimeMillis()));
// outStream.write(data);
// outStream.close();
// Log.d(TAG, "onPreviewFrame - wrote bytes: "
// + data.length);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// }
// Preview.this.invalidate();
// }
// });
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
camera.stopPreview();
camera.release();
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
camera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
camera.setPreviewDisplay(mHolder);
camera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
public void OLDsurfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = camera.getParameters();
//parameters.setPreviewSize(w, h);
//camera.setParameters(parameters);
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = sizes.get(0);
parameters.setPreviewSize(cs.width, cs.height);
//parameters.setRotation(90);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint p = new Paint(Color.RED);
Log.d(TAG, "draw");
canvas.drawText("PREVIEW", canvas.getWidth() / 2,
canvas.getHeight() / 2, p);
}
}
类预览扩展了SurfaceView实现了SurfaceHolder.Callback{
私有静态最终字符串TAG=“Preview”;
表面粗糙度;
公共摄像机;
预览(上下文,摄影机镜头){
超级(上下文);
摄像机=摄像机;
//安装SurfaceHolder.Callback,以便在
//创建和破坏下垫面。
mHolder=getHolder();
mHolder.addCallback(this);
//不推荐使用的设置,但在3.0之前的Android版本上是必需的
mHolder.setType(SurfaceHolder.SURFACE\u TYPE\u PUSH\u缓冲区);
}
已创建的公共空白表面(表面持有人){
试一试{
摄像头。设置预览显示(支架);
camera.startPreview();
}捕获(IOE异常){
Log.d(标记“错误设置相机预览:”+e.getMessage());
}
}
已创建的公共无效旧表面(表面旧支架){
//曲面已创建,获取摄影机并告知其位置
//画。
//camera=camera.open();
摄像头。设置显示方向(90);
试一试{
摄像头。设置预览显示(支架);
//camera.setPreviewCallback(新的PreviewCallback(){
//
//预览帧上的公共无效(字节[]数据,摄像机arg1){
//FileOutputStream扩展流=null;
//试一试{
//outStream=新文件OutputStream(String.format(
//“/sdcard/%d.jpg”,System.currentTimeMillis());
//外扩。写入(数据);
//exptream.close();
//d(标记“onPreviewFrame-写入的字节:”
//+数据长度);
//}catch(filenotfounde异常){
//e.printStackTrace();
//}捕获(IOE异常){
//e.printStackTrace();
//}最后{
// }
//Preview.this.invalidate();
// }
// });
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共空间表面覆盖(表面覆盖物持有人){
//当我们返回时,曲面将被破坏,因此请停止预览。
//因为CameraDevice对象不是共享资源,所以它非常有用
//当活动暂停时释放它很重要。
camera.stopPreview();
相机。释放();
}
公共无效表面更改(表面持有人,整数格式,整数w,整数h){
//如果预览可以更改或旋转,请在此处处理这些事件。
//请确保在调整预览大小或重新设置预览格式之前停止预览。
if(mHolder.getSurface()==null){
//预览曲面不存在
返回;
}
//在进行更改之前停止预览
试一试{
camera.stopPreview();
}捕获(例外e){
//忽略:尝试停止不存在的预览
}
//设置预览大小并进行任何调整大小、旋转或
//在此处重新格式化更改
//使用新设置开始预览
试一试{
摄像头。设置预览显示(mHolder);
camera.startPreview();
}捕获(例外e){
Log.d(标记“启动相机预览时出错:”+e.getMessage());
}
}
public void OLDsurfaceChanged(SurfaceHolder holder,int格式,int w,int h){
//现在已经知道大小,请设置相机参数并开始
//预览。
Camera.Parameters=Camera.getParameters();
//参数设置预览大小(w,h);
//设置参数(参数);
列表大小=参数。getSupportedPreviewSizes();
Camera.Size cs=Size.get(0);
parameters.setPreviewSize(cs.width,cs.height);
//参数设置旋转(90);
设置参数(参数);
camera.startPreview();
}
@凌驾
公共空白绘制(画布){
超级绘画(画布);
油漆p=新油漆(颜色为红色);
日志d(标签“图纸”);
canvas.drawText(“预览”,canvas.getWidth()/2,
canvas.getHeight()/2,p);
}
}
假设您在代码中为预览创建SurfaceView,并使用addView()将其添加到预览框架布局中,请将布局更改为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Empty layout where SurfaceView will be placed in code-->
</FrameLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/camera_photo"
android:id="@+id/take_photoimageButton"
android:visibility="visible"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
</LinearLayout>
就z顺序而言,在布局中稍后声明的视图绘制在先前声明的视图的顶部,而在代码中添加的视图绘制在顶部,就好像它们已添加到布局的末尾一样(除非指定索引)。放置空布局以添加SurfaceView,从而允许在ImageButton下绘制它
或者,您可以尝试调用索引为0的addView(),但问题是如何在布局XML中实现这一点
除非出于其他原因需要使用LinearLayout,否则可以将其删除,并将外部框架布局设置为顶级视图组。相机预览的
SurfaceView
在哪里?a、 与相机相关的布局?刚刚添加到问题中。