为什么不使用建议的 Google 方法并在 XML 中实现视图。创建两个自定义表面视图(在我的例子中,一个是 GLSurfaceView,另一个是 SurfaceView)。一个视图实现相机,第二个视图可以处理带有图形和/或按钮的 GUI。关键是在 XML 中创建自定义 GLSurfaceView 和 SurfaceView,使用扩展的 SurfaceView 和 GLSurfaceView 的类名。此外,您必须使 GUI 视图透明。请注意,图形是在 MainRenderer 类中绘制的,此处未包括。请注意,下面的代码存在 OpenGL 上下文丢失问题,我正在努力修复。下面显示的代码应该足以帮助您开始工作,但如果没有 mods,它将无法单独运行......下面的大部分代码都来自 Googles OpenGl Tutorial
OpenGL ES 1.0 教程
用我自己的模组把所有东西分开。
这是 XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.zypath.opengltest.MainSurfaceView
android:id="@+id/glSurfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.zypath.opengltest.CameraView
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:keepScreenOn="true" />
<LinearLayout
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/mangle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mangle"
android:textColor="@color/mcolor" />
</LinearLayout>
主要代码的一部分:
public class Main extends Activity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// When working with the camera, it's useful to stick to one orientation.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//Set full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
//Substantiate surfaces
cameraView = (CameraView) findViewById(R.id.camera);
mGLView = (MainSurfaceView) findViewById(R.id.glSurfaceView);
//Initialize TextViews
txtView = (TextView) findViewById(R.id.mangle);
txtView.setText("Angle: " + String.valueOf(mAngle));
}
自定义 MainSurfaceView 的一部分
public class MainSurfaceView extends GLSurfaceView {
...
//Constructor
public MainSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
mRenderer = new MainRenderer();
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
setRenderer(mRenderer);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
setZOrderOnTop(true);
//Render only when there is a change
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
//TouchEvent handler
@Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
.....
return true;
}
相机代码的一部分:
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
...
private Camera camera;
SurfaceHolder holder;
public CameraView(Context context, AttributeSet attrs) {
super(context, attrs);
// We're implementing the Callback interface and want to get notified
// about certain surface events.
getHolder().addCallback( this );
// We're changing the surface to a PUSH surface, meaning we're receiving
// all buffer data from another component - the camera, in this case.
getHolder().setType( SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS );
}
public void surfaceCreated( SurfaceHolder holder ) {
// Once the surface is created, simply open a handle to the camera hardware.
camera = Camera.open();
}
public void surfaceChanged( SurfaceHolder holder, int format, int width, int height ) {
camera.setPreviewDisplay( holder );
camera.startPreview();
}