save code
This commit is contained in:
@@ -6,16 +6,13 @@ import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.os.Debug;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.camera.core.Camera;
|
||||
import androidx.camera.core.CameraSelector;
|
||||
import androidx.camera.core.Preview;
|
||||
@@ -23,9 +20,6 @@ import androidx.camera.lifecycle.ProcessCameraProvider;
|
||||
import androidx.camera.view.PreviewView;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
import com.google.androidgamesdk.GameActivity;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
@@ -33,31 +27,23 @@ import com.google.mediapipe.examples.facelandmarker.FaceLandmarkerHelper;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class MainActivity extends GameActivity {
|
||||
|
||||
|
||||
private ExecutorService backgroundExecutor;
|
||||
private FaceLandmarkerHelper faceLandmarkerHelper;
|
||||
|
||||
private PreviewView previewView;
|
||||
private ProcessCameraProvider cameraProvider;
|
||||
private Camera camera;
|
||||
|
||||
private void initCamera(){
|
||||
// 添加 PreviewView 用于摄像头预览
|
||||
// 创建摄像头预览视图
|
||||
previewView = new PreviewView(this);
|
||||
previewView.setImplementationMode(PreviewView.ImplementationMode.COMPATIBLE);
|
||||
previewView.setScaleType(PreviewView.ScaleType.FILL_CENTER);
|
||||
|
||||
// 将 PreviewView 添加到布局中
|
||||
addContentView(previewView, new ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
ViewGroup.LayoutParams.MATCH_PARENT
|
||||
));
|
||||
|
||||
// ensureVulkanLayerTransparent();
|
||||
bringVulkanToFront();
|
||||
|
||||
// 检查权限并启动相机
|
||||
if (hasCameraPermission()) {
|
||||
startCamera();
|
||||
} else {
|
||||
@@ -65,6 +51,24 @@ public class MainActivity extends GameActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void bringVulkanToFront() {
|
||||
// 获取窗口的根视图
|
||||
View rootView = getWindow().getDecorView();
|
||||
if (rootView instanceof ViewGroup) {
|
||||
ViewGroup rootGroup = (ViewGroup) rootView;
|
||||
|
||||
for (int i = rootGroup.getChildCount() - 1; i >= 0; i--) {
|
||||
View child = rootGroup.getChildAt(i);
|
||||
if (child != previewView) {
|
||||
child.bringToFront();
|
||||
child.setBackgroundColor(Color.TRANSPARENT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
String native_lib_name = getResources().getString(R.string.native_lib_name);
|
||||
@@ -80,42 +84,8 @@ public class MainActivity extends GameActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
initCamera();
|
||||
|
||||
Log.i("MainActivity", "onCreate: ");
|
||||
|
||||
// 添加按钮到布局
|
||||
// addButtonToLayout();
|
||||
|
||||
// backgroundExecutor = Executors.newSingleThreadExecutor();
|
||||
// backgroundExecutor.execute(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// if (faceLandmarkerHelper.isClose()) {
|
||||
// faceLandmarkerHelper.setupFaceLandmarker();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
private void ensureVulkanLayerTransparent() {
|
||||
// 延迟执行以确保 GameActivity 的视图已经创建
|
||||
previewView.post(() -> {
|
||||
ViewGroup rootView = (ViewGroup) getWindow().getDecorView().getRootView();
|
||||
makeViewTransparent(rootView);
|
||||
});
|
||||
}
|
||||
|
||||
private void makeViewTransparent(ViewGroup viewGroup) {
|
||||
for (int i = 0; i < viewGroup.getChildCount(); i++) {
|
||||
View child = viewGroup.getChildAt(i);
|
||||
if (child instanceof ViewGroup) {
|
||||
makeViewTransparent((ViewGroup) child);
|
||||
} else {
|
||||
// 找到可能是 Vulkan 渲染层的视图并设置为透明
|
||||
if (child.getBackground() != null) {
|
||||
child.setBackgroundColor(Color.TRANSPARENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasCameraPermission() {
|
||||
@@ -139,19 +109,15 @@ public class MainActivity extends GameActivity {
|
||||
try {
|
||||
cameraProvider = cameraProviderFuture.get();
|
||||
|
||||
// 创建预览用例
|
||||
Preview preview = new Preview.Builder().build();
|
||||
|
||||
// 选择后置摄像头
|
||||
CameraSelector cameraSelector = new CameraSelector.Builder()
|
||||
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
|
||||
.build();
|
||||
|
||||
// 将预览绑定到 PreviewView
|
||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
||||
|
||||
// 绑定到生命周期
|
||||
camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview);
|
||||
cameraProvider.bindToLifecycle(this, cameraSelector, preview);
|
||||
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
Log.e("MainActivity", "Error starting camera: " + e.getMessage());
|
||||
@@ -185,42 +151,5 @@ public class MainActivity extends GameActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void addButtonToLayout() {
|
||||
// 创建按钮
|
||||
Button button = new Button(this);
|
||||
button.setText("Vulkan 按钮");
|
||||
|
||||
// 设置按钮位置(使用绝对坐标)
|
||||
button.setX(50); // 距离左边50像素
|
||||
button.setY(50); // 距离顶部50像素
|
||||
|
||||
// 设置按钮点击事件
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
onButtonClicked();
|
||||
}
|
||||
});
|
||||
|
||||
// 将按钮添加到 GameActivity 的布局中(不会覆盖 Vulkan 表面)
|
||||
addContentView(button, new ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
private void onButtonClicked() {
|
||||
Toast.makeText(this, "Vulkan 按钮被点击了!", Toast.LENGTH_SHORT).show();
|
||||
nativeButtonClicked();
|
||||
// 启动SecondActivity
|
||||
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
|
||||
// 可以传递数据
|
||||
intent.putExtra("message", "Hello from MainActivity!");
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
private native void nativeButtonClicked();
|
||||
|
||||
private native void sendArgumentsToPlatform(String[] args);
|
||||
}
|
||||
Reference in New Issue
Block a user