gpt4 book ai didi

android - 如何设置相机RelativeLayout的背景?

转载 作者:行者123 更新时间:2023-11-29 20:58:19 24 4
gpt4 key购买 nike

在我的应用程序中,我有这样的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
android:orientation="vertical">

<fragment xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
tools:context=".MapsActivity" />

</RelativeLayout>

如何将基本 RelativeLayout 布局的背景设置为相机 View ?

最佳答案

让你的 Root View 成为 SurfaceView:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<fragment
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/map"
android:layout_width="300dp"
android:layout_height="300dp"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>

然后在您的代码中,打开相机并将预览输出到您的 SurfaceView:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
SurfaceHolder surfaceHolder = surfaceView.getHolder();

surfaceHolder.addCallback(new SurfaceHolder.Callback() {
private Camera mCamera;
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();

try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
mCamera.startPreview();
}
});
}

关于android - 如何设置相机RelativeLayout的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26946817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com