我想知道何时从布局中实际创建了SurfaceView?
这是在Google Glass上的LiveCard服务中创建布局的场景 - 因此没有可以使用setContentView()设置此布局的活动。
仍然,我想知道如何以编程方式创建SurfaceView
我有以下布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<SurfaceView
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
然后我有:
mLayout = (FrameLayout) inflater.inflate(R.layout.orientation_recorder, null);
mLayout.setWillNotDraw(false);
cameraView = (SurfaceView) mLayout.findViewById(R.id.camera_view);
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e("OR", "camera view sufrace destroyed");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e("OR", "camera view sufrace created");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.e("OR", "camera view sufrace changed "
+ " format: " + format
+ " width: " + width
+ " height: " + height);
}
});
但是,我无法创建SurfaceView,例如从不在上面调用surfaceCreated()。
要做到这一点需要什么?
答案 0 :(得分:3)
您必须将内容视图设置为布局,并将曲面视图添加到活动或主要活动类中的布局
setContentView(R.layout.main);<---- basically points to and uses your xml
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);<---- points to Layout defined in xml
layout.addView(new SurfaceView(this));<----adds SurfaceView to layout
您可以在主要活动
中执行此操作或此调用view surface = new SurfaceView(this);
SetContentView(surface);